//Timestamp to HH:mm:ss format
Long Timestamp = 1633304782;
Date timeD = new Date(Timestamp * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String Time = sdf.format(timeD);
//RESULT this code convert 1633304782 to 05:46:33
import java.sql.Timestamp;
public class StringToTimeStamp {
public static void main(String[] args) {
String dateTime = "2020-12-12 01:24:23";
Timestamp timestamp = Timestamp.valueOf(dateTime);
System.out.println(timestamp);
}
}
String timestampToDate(long timestamp, String pattern)
{
Date timeD = new Date(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(timeD);
}
//You can use this for both date or time.
// long timestamp = System.currentTimeMillis();
// eg: for date use it like this:
//timestampToDate(timestamp, "dd MMM, yyyy");
// eg: for time use it like this:
//timestampToDate(timestamp, "hh:mm a");
// eg: for both:
//timestampToDate(timestamp, "dd MMM, yyyy - hh:mm a");
All you need to do is change the string within the java.text.SimpleDateFormat constructor to: "MM-dd-yyyy HH:mm:ss".
Just use the appropriate letters to build the above string to match your input date.