java - 以毫秒为单位格式化时间后出现奇怪的输出

标签 java date simpledateformat

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

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

我想得到输出:Wednesday 20 November, 2019但我得到 Monday 19 January, 1970 。如何获取当前日期而不是 1970 年代的日期?

最佳答案

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

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

此代码(在我的系统上)产生的输出是

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,那么您可以使用 ThreeTenBackport-Project on Github ,它在这两个旧版本中启用了 java.time 的(大部分)功能。
其用途解释on a separate website .

关于java - 以毫秒为单位格式化时间后出现奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58949399/

相关文章:

android - Android 中的解析异常

java - 使用什么 SimpleDateFormat 模式?

java - 在接口(interface)中实现 Comparable

Java 将 char 从方法传递回 main

Swift - 打印今年的当前星期几

java - 从表单中的 String 获取 Date 对象 2012-07-26 23 :59:59

javascript - 在 javascript 中验证此 "dd-MMM-yyyy"格式的两个日期

java - ManagementFactory.getRuntimeMXBean().getUptime() 格式化以显示日、小时、分钟和秒

java - 如何运行服务器的批处理文件(永久运行)并将控制权返回给java程序而不关闭该批处理

java - 寻求 Java 代码架构方面的帮助