Failing to convert from epochmili to string

98 Views Asked by At

This the epoch mili - 1526581800000

The code which am using for conversion is -

LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(1526581800000),ZoneId.systemDefault());      
System.out.println(localDateTime.format(DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss")));

Output which we getting- 17-May-2018 18:30:00

But Expected Output - 18-May-2018 00:00:00

1

There are 1 best solutions below

2
On

Well, as already mentioned in a comment, your call to ZoneId.systemDefault() does not return the expected ZoneId. That means you either have to adjust the configuration of your system/JVM or specify a ZoneId manually, like this:

public static void main(String[] args) {
    LocalDateTime localDateTime = LocalDateTime.ofInstant(
                                      Instant.ofEpochMilli(1526581800000L),
                                      ZoneId.of("Asia/Kolkata") // SPECIFY ZONE
                                  );
    System.out.println(
            localDateTime.format(
                    DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss",
                                                Locale.ENGLISH)
            )
    );
}

Output (totally independent from the system's time zone):

18-May-2018 00:00:00