Today we are going to see how to Convert 13 Digit Timestamp to datetime Java. Converting a timestamp to datetime is quite easy if you are aware of Java.
If you can use Java 8’s new Time API, then you can create an instant and convert that it to the desired datetime (your computer time zone in my example below):
Instant instant = Instant.ofEpochMilli(millis);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(fmt.format(instant.atZone(ZoneId.systemDefault())));
Below we are converting a UNIX timestamp to date Java
import java.util.*; import java.text.*; public class Exercise36 { public static void main(String[] args) { //Unix seconds long unix_seconds = 1372339860; //convert seconds to milliseconds Date date = new Date(unix_seconds*1000L); // format of the date SimpleDateFormat jdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); jdf.setTimeZone(TimeZone.getTimeZone("GMT-4")); String java_date = jdf.format(date); System.out.println("\n"+java_date+"\n"); } }
There are few other methods which we can use to convert the timestamp to datetime in java which we will be updating shortly.
Comments