xxxxxxxxxx
import java.util.Date;
public class Main {
public static void main(String[] args) {
// Assuming you have a timestamp in milliseconds
long timestamp = 1609459200000L;
// Create a new Date object using the timestamp
Date date = new Date(timestamp);
// Print the date using a desired format
System.out.println(date.toString());
}
}
xxxxxxxxxx
//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
xxxxxxxxxx
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");
xxxxxxxxxx
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.