格式化 DateTime 时如何应用时区?

How to apply timezone when formatting DateTime?

我的日期时间为 2011-01-11 01:51:10,时区为 America/Los_Angeles

我想获取此值的本地化日期时间。这就是我所做的

val formatter1: DateTimeFormatter = DateTimeFormatter.ofPattern("y-M-d H:m:s");
val m1: LocalDateTime = LocalDateTime.parse("2011-01-11 01:51:10", formatter1);
println("DateTime: " + m1.atZone(ZoneId.of("America/Los_Angeles")))

我得到的值是

DateTime: 2011-01-11T01:51:10-08:00[America/Los_Angeles]

如何将其转换为应用了 -08:00 偏移量且未应用 [America/Los_Angeles] 的本地化日期时间?

可能您想要的是获取 UTC 时间,然后对其应用时区偏移。 使用 Joda 时间很容易做到。例如:

DateTime.now().minus(timezoneOffset) 

其中 timezoneOffset 是代表您所在位置的时移的整数。如果我错了纠正我。

看起来您正在使用具有 ZonedDateTime 的 java.time API。您应该使用它而不是 LocalDateTime,因为 LocalDateTime 没有时区。来自文档:

A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.

This class does not store or represent a time or time-zone. Instead, it is a description of the date, as used for birthdays. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.

然后,ZonedDateTime docs states that:

A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.

This class handles conversion from the local time-line of LocalDateTime to the instant time-line of Instant. The difference between the two time-lines is the offset from UTC/Greenwich, represented by a ZoneOffset.

使用 ZonedDateTime,您的代码如下:

import java.time._
import java.time.format._

val zoneId = ZoneId.of("America/Los_Angeles")
val formatter = DateTimeFormatter.ofPattern("y-M-d H:m:s").withZone(zoneId)
val zdt = ZonedDateTime.parse("2011-01-11 01:51:10", formatter)

您将在控制台看到的结果将是:

zdt: java.time.ZonedDateTime = 2011-01-11T01:51:10-08:00[America/Los_Angeles]

发生这种情况是因为您使用的 default toString method of ZonedDateTime and looks like the DateTimeFormatter.ISO_OFFSET_DATE_TIME 正是您想要的。所以你的代码应该是:

import java.time._
import java.time.format._

val zoneId = ZoneId.of("America/Los_Angeles")
val formatter = DateTimeFormatter.ofPattern("y-M-d H:m:s").withZone(zoneId)
val zdt = ZonedDateTime.parse("2011-01-11 01:51:10", formatter)
val formatted: String = zdt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)

您首先必须指定您解析的时间所在的时区。然后指定要转换成的另一个时区。

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("y-M-d H:m:s");
LocalDateTime m1 = LocalDateTime.parse("2011-01-11 01:51:10", formatter1);
ZonedDateTime z1 = m1.atZone(ZoneId.of("UTC"));
ZonedDateTime z2 = z1.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));

System.out.println(z2.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

请查看我对此的完整回答。 Answer

    String dateTime = "MM/dd/yyyy HH:mm:ss";
    String date = "09/17/2017 20:53:31";
    Integer gmtPSTOffset = -8;
    ZoneOffset offset = ZoneOffset.ofHours(gmtPSTOffset);

    // String to LocalDateTime
    LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(dateTime));
    // Set the generated LocalDateTime's TimeZone. In this case I set it to UTC
    ZonedDateTime ldtUTC = ldt.atZone(ZoneOffset.UTC);
    System.out.println("UTC time with Timezone          : "+ldtUTC);

    // Convert above UTC to PST. You can pass ZoneOffset or ZoneId for 2nd parameter
    LocalDateTime ldtPST = LocalDateTime.ofInstant(ldtUTC.toInstant(), offset);
    System.out.println("PST time without offset         : "+ldtPST);

    // If you want UTC time with timezone
    ZoneId zoneId = ZoneId.of( "America/Los_Angeles" );
    ZonedDateTime zdtPST = ldtUTC.toLocalDateTime().atZone(zoneId);
    System.out.println("PST time with Offset and TimeZone   : "+zdtPST);