简而言之:
我有一个纪元时间,我想让它从 January 1, 1970, 00:00:00 GMT
开始如java.util.Date
期望得到。
此代码有助于演示我的问题:
import java.util.Date;
import java.util.Locale;
import org.junit.Test;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class TimeHelp {
private String format = "EEE MMM dd HH:mm:ss yyyy";
public SimpleDateFormat asSimpleDateFormat() {
return new SimpleDateFormat(format, Locale.ENGLISH);
}
public DateTimeFormatter asDateTimeFormatter() {
return DateTimeFormatter.ofPattern(format, Locale.ENGLISH);
}
@Test
public void test() throws Exception {
System.out.println(ZoneId.systemDefault());
String s = "Sun Apr 04 02:00:01 2010";
long t1 = asSimpleDateFormat().parse(s).getTime();
ZonedDateTime zoned = LocalDateTime.parse(s, asDateTimeFormatter())
.atZone(ZoneId.systemDefault());
long t2 = zoned.toEpochSecond() * 1000;
long t3 = Date.from(zoned.toInstant()).getTime();
long t4 = zoned.toInstant().toEpochMilli();
System.out.println("t1 " + t1);
System.out.println("t2 " + t2);
System.out.println("t3 " + t3);
System.out.println("t4 " + t4);
System.out.println("Difference in minutes " + Math.abs(t1 - t2)/1000/60);
}
}
输出:
Australia/Sydney
t1 1270310401000
t2 1270306801000
t3 1270306801000
t4 1270306801000
Difference in minutes 60
请注意,t1 与其他所有不同,我认为是因为 t1 是 GMT,而其他都是 UTC。
如果我使用SimpleDateFormat
long 的值与我使用 DateTimeFormatter
时不同获得 ZonedDateTime
之后我调用 toEpochSecond()
.
出于某种原因,我希望获得 ZonedDateTime
我想将其转换为 Date
但看起来这样的事情不会起作用,因为 Date
在 GMT 中工作,不适用于 UTC
.
最佳答案
引用timeanddate.com :
When local daylight time was about to reach
Sunday, April 4, 2010, 3:00:00 am
clocks were turned backward 1 hour toSunday, April 4, 2010, 2:00:00 am
local standard time instead.
这意味着 Sun Apr 04 02:00:01 2010
那天发生了两次。那么您会得到这两个中的哪一个?
使用SimpleDateFormat
,您可以获得稍后的一个,尽管没有记录。
与 ZonedDateTime
你得到了较早的一个:
For Overlaps, the general strategy is that if the local date-time falls in the middle of an Overlap, then the previous offset will be retained. If there is no previous offset, or the previous offset is invalid, then the earlier offset is used, typically "summer" time.. Two additional methods,
withEarlierOffsetAtOverlap()
andwithLaterOffsetAtOverlap()
, help manage the case of an overlap.
关于java - 在Java中,如何将自纪元以来的秒数转换为自1970年1月1日以来的毫秒数,00 :00:00 GMT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60199393/