格式化时间后的奇怪输出(以毫秒为单位)

格式化时间后的奇怪输出(以毫秒为单位)

问题描述:

我想使用代码将 1574348400 的值转换为日期格式:

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();
    }
}

我想得到的输出为: 2019年11月20日星期三,但是我要获得 1970年1月19日星期一.如何获取当前日期而不是1970年的日期?

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?

使用 java.time 解析您的时间(以秒为单位),它提供了一种以秒为单位的方法...

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

from seconds:   2019-11-21T16:00:00+01:00[Europe/Berlin]
from millis:    2019-11-21T16:00:00+01:00[Europe/Berlin]
Both ZonedDateTimes are equal
————————————————————————————————
from seconds:   2019-11-21T15:00:00Z[UTC]
from millis:    2019-11-21T15:00:00Z[UTC]
Both ZonedDateTimes are equal

如果您必须使用Java 6或7,则可以在Github上使用 ThreeTenBackport-Project,它在这两个旧版本中启用了 java.time 的(大多数)功能.
在单独的网站上说明.

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.