使用新的API,我可以获得本地日期和时间:
LocalTime localTime = LocalTime.of(9,0,0);
LocalDate localDate = LocalDate.of(2017, Month.JUNE, 3);
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
System.out.println("LocalDateTime:" + localDateTime);
如果我需要将时间转换为特定的时区,我可以使用ZoneId:
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("GMT"));
System.out.println("ZonedDateTime: " + zonedDateTime);
ZonedDateTime zonedDateTime2 = ZonedDateTime.of(localDateTime, ZoneId.of("Europe/London"));
System.out.println("ZonedDateTime London: " + zonedDateTime2);
我可以通过以下方式获取当前时间:
Instant currentTime = Instant.now();
我的问题。 使用这个新的API,是否可以获取客户端机器上当前使用的时区ID和数字定义(例如+04:00)?
ZoneId.systemDefault().getRules().getOffset(Instant.now());
。但是您应该知道,此偏移量取决于当前时间(夏令时等)。 - Alexander Terekhov