当前位置:  开发笔记 > 编程语言 > 正文

Weird output after formatting the time in milliseconds

如何解决《Weirdoutputafterformattingthetimeinmilliseconds》经验,为你挑选了1个好方法。

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?



1> deHaar..:

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.

推荐阅读
sx-March23
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有