在Java 8中获取不同时区的当前时间

16

我正在探索Java 8的新java.time API。我特别想获取当前时间(我的当前时区,另一个时区和不同偏移量的时间)。

代码如下:

public static void getCurrentLocalTime(){
    LocalTime time = LocalTime.now();
    System.out.println("Local Time Zone: "+ZoneId.systemDefault().toString());
    System.out.println("Current local time : " + time);
}

public static void getCurrentTimeWithTimeZone(){
    LocalDateTime localtDateAndTime = LocalDateTime.now();
    ZoneId zoneId = ZoneId.of("America/Los_Angeles");
    ZonedDateTime dateAndTimeInLA  = ZonedDateTime.of(localtDateAndTime, zoneId);
    String currentTimewithTimeZone =dateAndTimeInLA.getHour()+":"+dateAndTimeInLA.getMinute();
    System.out.println("Current time in Los Angeles: " + currentTimewithTimeZone);
}

public static void getCurrentTimeWithZoneOffset(){
    LocalTime localtTime = LocalTime.now();
    ZoneOffset offset = ZoneOffset.of("-08:00");
    OffsetTime  offsetTime  = OffsetTime.of(localtTime, offset);
    String currentTimewithZoneOffset =offsetTime.getHour()+":"+offsetTime.getMinute();
    System.out.println("Current time  with offset -08:00: " + currentTimewithZoneOffset);
}

但是,当我调用这些方法时,我得到的是相同的时间(即我的系统时间),显然不符合我的期望。

方法调用的输出:

Current time in Los Angeles: 19:59
Local Time Zone: Asia/Calcutta
Current local time : 19:59:20.477
Current time  with offset -08:00: 19:59

即使我设置了不同的时区和偏移量,为什么我仍然得到相同的时间?


看起来非常接近于https://dev59.com/rm435IYBdhLWcg3w2z98 - mkobit
已编辑。感谢指出。 - user2693135
3个回答

25
LocalDateTime.now()永远以您的默认时区返回当前日期/时间(例如,在伦敦,10月13日上午11:20)。当您使用特定的ZoneIdZoneOffset创建ZonedDateTimeOffsetTime时,您会得到相同的日期和时间,但在不同的时区中(例如,在洛杉矶,10月13日上午11:20),这代表了不同的时间瞬间。您可能正在寻找类似于:
Instant now = Instant.now();
ZoneId zoneId = ZoneId.of("America/Los_Angeles");
ZonedDateTime dateAndTimeInLA = ZonedDateTime.ofInstant(now, zoneId);

这将计算洛杉矶的当前日期和时间:10月13日,上午3点20分。


10

还有另一种显示另一个时区时间的方式,可以使用withZoneSameInstant

// now, in current zone,
// 2016-04-13T14:24:57.618+02:00[Europe/Warsaw]
ZonedDateTime now = ZonedDateTime.now();

// 2016-04-13T05:24:57.618-07:00[America/Los_Angeles]
ZonedDateTime losAngelesDateTime = now.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));

9

考虑下面这些固定的方法:

public static void getCurrentLocalTime() {
    LocalTime time = LocalTime.now();
    System.out.println("Local Time Zone: " + ZoneId.systemDefault().toString());
    System.out.println("Current local time : " + time);
}

public static void getCurrentTimeWithTimeZone() {
    LocalDateTime localDateAndTime = LocalDateTime.now(ZoneId.of("America/Los_Angeles"));
    System.out.println("Current time in Los Angeles: " + localDateAndTime.toLocalTime());
}

public static void getCurrentTimeWithZoneOffset() {
    LocalTime localTime = LocalTime.now(ZoneOffset.of("-08:00"));
    System.out.println("Current time  with offset -08:00: " + localTime);
}

改变的是,现在调用的是now(zone)而不是now()。这是因为now()总是返回您所在时区的当前系统时间。对atZoneOffsetTime.ofZoneDateTime.of的调用不会改变日期/时间,它只告诉Java Time应该在这个时区理解日期。

在调用这三种方法时,在我的计算机上输出如下:

Local Time Zone: Europe/Paris
Current local time : 12:32:21.560
Current time in Los Angeles: 03:32:21.579
Current time  with offset -08:00: 02:32:21.580

为了让这个概念更清晰:假设你在欧洲,并调用LocalDateTime.now().atZone(ZoneId.of("America/Los_Angeles")) - 那么你会创建一个日期/时间,它代表了当前欧洲的时间,就像你位于洛杉矶一样。因此,你创建的日期/时间实际上对于洛杉矶居民来说是未来的(根据夏令时的不同,会相差8或9个小时)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接