获取Java 8中的当前时间

use*_*135 15 java java-8 java-time

我正在探索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);
}
Run Code Online (Sandbox Code Playgroud)

但是,当我调用这些方法时,我会得到相同的时间(我的系统时间),这显然不是我所期待的.

方法的输出调用:

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
Run Code Online (Sandbox Code Playgroud)

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

ass*_*ias 21

LocalDateTime.now()始终返回默认时区的当前日期/时间(例如10月13日伦敦上午11点20分).当您创建ZonedDateTimeOffsetTime从它与特定的ZoneIdZoneOffset,你会得到相同的日期和时间,但(在洛杉矶上午11时20分例如10月13日),这是时间上的不同时刻的不同的时区.

你可能正在寻找类似的东西:

Instant now = Instant.now();
ZoneId zoneId = ZoneId.of("America/Los_Angeles");
ZonedDateTime dateAndTimeInLA = ZonedDateTime.ofInstant(now, zoneId);
Run Code Online (Sandbox Code Playgroud)

这将计算洛杉矶的当前日期和时间:10月13日凌晨3点20分.


Tun*_*aki 7

请考虑以下固定方法:

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);
}
Run Code Online (Sandbox Code Playgroud)

改变的是,不是打电话now(),而是打电话now(zone).这是因为now()始终在您的时区中返回当前系统时间.到通话atZone,OffsetTime.ofZoneDateTime.of不更改日期/时间,它只是告诉Java的时间日期应该在这个时区的日期/时间来理解.

调用这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
Run Code Online (Sandbox Code Playgroud)

为了说清楚这一点:你在欧洲打电话LocalDateTime.now().atZone(ZoneId.of("America/Los_Angeles"))- 你正在创建一个日期/时间,代表你在欧洲的当前时间,就像你在洛杉矶一样,所以你创建的日期/时间实际上洛杉矶居民的未来(未来 8或9小时,取决于夏令时).


mle*_*ski 6

还有另一种使用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"));
Run Code Online (Sandbox Code Playgroud)