I want to convert 1574348400
value to date format using code:
public class Main { public Main() { long value = 1574348400; String dateString = new SimpleDateFormat("EEEE dd MMMM, yyyy").format(new Date(value)); System.out.println("Formated time: " + dateString); } public static void main(String[] args) { new Main(); } }
I want to get the output as: Wednesday 20 November, 2019
but I'm getting Monday 19 January, 1970
. How to get the current date not the 1970's date?
Parse your time (in seconds) using java.time
, it provides a method for epoch seconds...
public static void main(String[] args) { // your seconds long seconds = 1574348400; // same in millis long millis = 1574348400000L; // find out the zone of your system ZoneId systemDefaultZoneId = ZoneId.systemDefault(); // or set a specific one ZoneId utcZoneId = ZoneId.of("UTC"); // parse a ZonedDateTime of your system default time zone from the seconds ZonedDateTime fromSecsSysDefZone = ZonedDateTime.ofInstant(Instant.ofEpochSecond(seconds), systemDefaultZoneId); // parse a ZonedDateTime of UTC from the seconds ZonedDateTime fromSecsUtc = ZonedDateTime.ofInstant(Instant.ofEpochSecond(seconds), utcZoneId); // parse a ZonedDateTime of your system default time zone from the milliseconds ZonedDateTime fromMillisSysDefZone = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), systemDefaultZoneId); // parse a ZonedDateTime of UTC from the milliseconds ZonedDateTime fromMillisUtc = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), utcZoneId); // print the ones that were created using your default time zone System.out.println("from seconds:\t" + fromSecsSysDefZone.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)); System.out.println("from millis:\t" + fromMillisSysDefZone.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)); // print a check for equality System.out.println("Both ZonedDateTimes are " + (fromSecsSysDefZone.equals(fromMillisSysDefZone) ? "equal" : "different")); System.out.println("————————————————————————————————"); // print the ones that were created using UTC System.out.println("from seconds:\t" + fromSecsUtc.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)); System.out.println("from millis:\t" + fromMillisUtc.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)); // print a check for equality System.out.println("Both ZonedDateTimes are " + (fromSecsUtc.equals(fromMillisUtc) ? "equal" : "different")); }
The output produced by this code (on my system) is
public static void main(String[] args) { // your seconds long seconds = 1574348400; // same in millis long millis = 1574348400000L; // find out the zone of your system ZoneId systemDefaultZoneId = ZoneId.systemDefault(); // or set a specific one ZoneId utcZoneId = ZoneId.of("UTC"); // parse a ZonedDateTime of your system default time zone from the seconds ZonedDateTime fromSecsSysDefZone = ZonedDateTime.ofInstant(Instant.ofEpochSecond(seconds), systemDefaultZoneId); // parse a ZonedDateTime of UTC from the seconds ZonedDateTime fromSecsUtc = ZonedDateTime.ofInstant(Instant.ofEpochSecond(seconds), utcZoneId); // parse a ZonedDateTime of your system default time zone from the milliseconds ZonedDateTime fromMillisSysDefZone = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), systemDefaultZoneId); // parse a ZonedDateTime of UTC from the milliseconds ZonedDateTime fromMillisUtc = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), utcZoneId); // print the ones that were created using your default time zone System.out.println("from seconds:\t" + fromSecsSysDefZone.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)); System.out.println("from millis:\t" + fromMillisSysDefZone.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)); // print a check for equality System.out.println("Both ZonedDateTimes are " + (fromSecsSysDefZone.equals(fromMillisSysDefZone) ? "equal" : "different")); System.out.println("————————————————————————————————"); // print the ones that were created using UTC System.out.println("from seconds:\t" + fromSecsUtc.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)); System.out.println("from millis:\t" + fromMillisUtc.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)); // print a check for equality System.out.println("Both ZonedDateTimes are " + (fromSecsUtc.equals(fromMillisUtc) ? "equal" : "different")); }
If you have to use Java 6 or 7, then you can use the ThreeTenBackport-Project on Github, which enables (most) functionality of java.time
in those two older versions.
Its use is explained on a separate website.