15

我正在使用Java 8
这就是我的ZonedDateTime样子

2013-07-10T02:52:49+12:00

我得到这个值

z1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)

哪里z1ZonedDateTime

我想将此值转换为2013-07-10T14:52:49

我怎样才能做到这一点?

4

4 回答 4

17

这是你想要的吗?通过将您转换为之前,这会将您转换ZonedDateTimeLocalDateTime具有给定值的 a 。ZoneIdZonedDateTimeInstant

LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneOffset.UTC);

或者,也许您想要用户系统时区而不是硬编码的 UTC:

LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneId.systemDefault());
于 2016-02-28T23:21:29.427 回答
11

看起来您需要在将其发送到格式化程序之前转换为所需的时区 (UTC)。

z1.withZoneSameInstant( ZoneId.of("UTC") )
  .format( DateTimeFormatter.ISO_OFFSET_DATE_TIME )

应该给你类似的东西2018-08-28T17:41:38.213Z

于 2018-08-28T17:51:41.427 回答
4

@SimMac 感谢您的澄清。我也面临同样的问题,并能够根据他的建议找到答案。

public static void main(String[] args) {
    try {
        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 Zone 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);

    } catch (Exception e) {
    }
}

输出:

UTC time with Timezone          : 2017-09-17T20:53:31Z
PST time without offset         : 2017-09-17T12:53:31
PST time with Offset and TimeZone   : 2017-09-17T20:53:31-08:00[America/Los_Angeles]
于 2017-12-07T00:29:16.617 回答
2

如果z1是 的一个实例ZonedDateTime,则表达式

z1.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime()

评估为具有LocalDateTimeOP 请求的字符串表示形式的实例。以下程序说明了这一点:

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class Main {

  public static void main(String[] args) {
    ZonedDateTime time = ZonedDateTime.now();
    ZonedDateTime truncatedTime = time.truncatedTo(ChronoUnit.SECONDS);
    ZonedDateTime truncatedTimeUtc = truncatedTime.withZoneSameInstant(ZoneOffset.UTC);
    LocalDateTime truncatedTimeUtcNoZone = truncatedTimeUtc.toLocalDateTime();

    System.out.println(time);
    System.out.println(truncatedTime);
    System.out.println(truncatedTimeUtc);
    System.out.println(truncatedTimeUtcNoZone);
  }
}

样本输出:

2020-10-26T16:45:21.735836-03:00[America/Sao_Paulo]
2020-10-26T16:45:21-03:00[America/Sao_Paulo]
2020-10-26T19:45:21Z
2020-10-26T19:45:21
于 2020-10-26T20:19:21.370 回答