gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:21:20 24 4
gpt4 key购买 nike

我要转换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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com