繁体   English   中英

应用偏移量的 ZonedDateTime 到 UTC?

[英]ZonedDateTime to UTC with offset applied?

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

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

我得到这个值

z1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)

其中z1ZonedDateTime

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

我怎样才能做到这一点?

这是你想要的吗? 通过将ZonedDateTime转换为Instant before,这会将您的ZonedDateTime转换为具有给定ZoneIdLocalDateTime

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

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

LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneId.systemDefault());

看起来您需要先转换为所需的时区 (UTC),然后才能将其发送到格式化程序。

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

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

@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]

如果z1ZonedDateTime的实例,则表达式

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

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

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 = trucatedTime.withZoneSameInstant(ZoneOffset.UTC);
    LocalDateTime truncatedTimeUtcNoZone = truncatedTimeUtc.toLocalDateTime();

    System.out.println(time);
    System.out.println(trucatedTime);
    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

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

2013-07-10T02:52:49+12:00 ≠ 2013-07-10T14:52:49 在 UTC

2013-07-10T02:52:49+12:00 = 2013-06-09T14:52:49 在 UTC(通过从 2013-07-10T02:52:49 减去 12:00 时的偏移量获得)。

演示:

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

class Main {
    public static void main(String[] args) {
        ZoneOffset zoneOffset = ZoneOffset.of("+12:00");
        OffsetDateTime odtGiven = OffsetDateTime.of(LocalDateTime.of(2013, 7, 10, 2, 52, 49), zoneOffset);
        System.out.println(odtGiven);

        OffsetDateTime odtUtc = odtGiven.withOffsetSameInstant(ZoneOffset.UTC);
        System.out.println(odtUtc);
        System.out.println(odtUtc.toLocalDateTime());
    }
}

Output :

2013-07-10T02:52:49+12:00
2013-07-09T14:52:49Z
2013-07-09T14:52:49

Trail: Date Time了解现代日期时间 API

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM