简体   繁体   English

带ZoneOffset的时间戳格式

[英]Timestamp format with ZoneOffset

I want to print now() with different time offsets based on UTC: 我想根据UTC使用不同的时间偏移量来打印now():

offset=ZoneOffset.ofHours(-1);
zoneId=ZoneId.ofOffset("UTC", offset);
timeStampformater=DateTimeFormatter.ofPattern("yyyy.MM.dd.hh.mm.ss").withZone(zoneId);
System.out.println(LocalDateTime.now().format(timeStampformater));

offset=ZoneOffset.ofHours(0);
zoneId=ZoneId.ofOffset("UTC", offset);
timeStampformater=DateTimeFormatter.ofPattern("yyyy.MM.dd.hh.mm.ss").withZone(zoneId);
System.out.println(LocalDateTime.now().format(timeStampformater));

offset=ZoneOffset.ofHours(1);
zoneId=ZoneId.ofOffset("UTC", offset);
timeStampformater=DateTimeFormatter.ofPattern("yyyy.MM.dd.hh.mm.ss").withZone(zoneId);
System.out.println(LocalDateTime.now().format(timeStampformater));

However it always prints the same local time: 但是,它始终显示相同的本地时间:

2015.12.15.09.56.00  -> should be 7:56
2015.12.15.09.56.00  -> should be 8:56
2015.12.15.09.56.00  -> OK because it is the local time.

The method DateTimeFormatter.withZone does not change the date/time. DateTimeFormatter.withZone方法不会更改日期/时间。 Internally, the formatter sets the timezone to the date/time to format and then formats it. 在内部,格式化程序将时区设置为要格式化的日期/时间,然后对其进行格式化。 Quoting its Javadoc: 引用其Javadoc:

In all other cases, the override zone is added to the temporal, replacing any previous zone, but without changing the date/time. 在所有其他情况下,将覆盖区域添加到时态中,替换以前的任何区域,但不更改日期/时间。

LocalDateTime.now() always returns the current date/time in your default timezone. LocalDateTime.now()始终返回默认时区中的当前日期/时间。 Setting a timezone with a specific ZoneId or ZoneOffset (using atZone to create a ZonedDateTime ), you get the same date/time but in a different time zone (so it is a different instant in time). 使用特定的ZoneIdZoneOffset设置时区(使用atZone创建ZonedDateTime ),您将获得相同的日期/时间,但处于不同的时区(因此时间是不同的时刻)。

Instead, you should construct the LocalDateTime by specifying the ZoneId or ZoneOffset using LocalDateTime.now(ZoneId) : 相反,您应该通过使用LocalDateTime.now(ZoneId)指定ZoneIdZoneOffset来构造LocalDateTime

offset = ZoneOffset.ofHours(-1);
zoneId = ZoneId.ofOffset("UTC", offset);
timeStampformater = DateTimeFormatter.ofPattern("yyyy.MM.dd.hh.mm.ss");
System.out.println(LocalDateTime.now(zoneId).format(timeStampformater));

offset = ZoneOffset.ofHours(0);
zoneId = ZoneId.ofOffset("UTC", offset);
timeStampformater = DateTimeFormatter.ofPattern("yyyy.MM.dd.hh.mm.ss");
System.out.println(LocalDateTime.now(zoneId).format(timeStampformater));

offset = ZoneOffset.ofHours(1);
zoneId = ZoneId.ofOffset("UTC", offset);
timeStampformater = DateTimeFormatter.ofPattern("yyyy.MM.dd.hh.mm.ss");
System.out.println(LocalDateTime.now(zoneId).format(timeStampformater));

Note that you could simply your code by not using the ZoneId variable. 请注意,您可以不使用ZoneId变量来简单地编写代码。

offset = ZoneOffset.ofHours(-1);
timeStampformater = DateTimeFormatter.ofPattern("yyyy.MM.dd.hh.mm.ss");
System.out.println(LocalDateTime.now(offset).format(timeStampformater));

You don't need the intermediate ZoneId object - and if you are interested in offset times, you could use an OffsetDateTime directly and apply an offset to that instead of applying an offset to the formatter (the rules for the application of the offset by the formatter are somewhat convoluted). 您不需要中间的ZoneId对象-如果您对偏移量时间感兴趣,则可以直接使用OffsetDateTime并对其应用偏移量,而不是对格式程序应用偏移量(通过格式化程序有些复杂)。

Something like: 就像是:

DateTimeFormatter timeStampformater = DateTimeFormatter.ofPattern("yyyy.MM.dd.hh.mm.ss");

ZoneOffset offset = ZoneOffset.ofHours(-1);
System.out.println(OffsetDateTime.now(offset).format(timeStampformater));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM