在Java 8中获取当前时间

我正在探索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 

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

LocalDateTime.now()始终返回默认时区中的当前日期/时间(例如10月13日伦敦上午11点20分)。 当您使用特定的ZonedDateTimeOffsetTime从中创建ZoneIdZoneOffset ,您将获得相同的日期和时间,但是在不同的时区(例如洛杉矶的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分。

考虑以下固定方法:

 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() ,而是调用now(zone) 。 这是因为now()始终返回您所在时区的当前系统时间。 对atZoneOffsetTime.ofZoneDateTime.of的调用不会更改日期/时间,它只告诉Java Time应该在此时区的日期/时间理解日期。

调用这3种方法时,这是我机器上的输出:

 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个小时,具体取决于夏令时)。

还有另一种使用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"));