【问题标题】:Convert A DateTime With Given Offset to UTC Java将具有给定偏移量的日期时间转换为 UTC Java
【发布时间】:2019-09-25 12:01:01
【问题描述】:

鉴于我有字符串“2019-11-05/23:00”和偏移量“+07:00”,

我将如何在 Java(8) 中将其转换为 LocalDateTime UTC。

我当前的代码

@GetMapping("postDate/{date}")
public void testPost(@RequestParam("timezone") String timeZone, @PathVariable String date) {
    String format = "yyyy-MM-dd-HH:mm";
    String offset = timeZone;
    System.out.println(date);
    LocalDateTime timeWithOffset = LocalDateTime.parse(date,
                                                       DateTimeFormatter.ofPattern(format));

    System.out.println("\n\n\n" + timeWithOffset + "\n\n\n");

    // Cant Figure Out to get LocalDateTime timeInUTC
}

邮递员的请求

http://localhost:8080/postDate/2019-11-05-23:00?timezone=+07:00

【问题讨论】:

  • 您可以使用ZonedDateTimeOffsetDateTime 并将时区应用为ZoneId,您可以通过传递给此方法的参数String timeZone 获得。后者可能会因为此参数中可能未知的时区而失败。
  • 关于输入字符串的格式,我建议改用标准 ISO 8601 格式。您已经非常接近符合标准了。

标签: java datetime java-8 datetimeoffset


【解决方案1】:

您可以使用java.time 中的区域感知和偏移感知类。你会得到一个偏移量(我建议相应地命名参数,因为偏移量不是时区)和日期时间,这足以将同一时刻转换为不同的时区或偏移量。

看看这个例子并阅读 cmets:

public static void main(String[] args) {
    // this simulates the parameters passed to your method
    String offset = "+07:00";
    String date = "2019-11-05/23:00";

    // create a LocalDateTime using the date time passed as parameter
    LocalDateTime ldt = LocalDateTime.parse(date,
                                            DateTimeFormatter.ofPattern("yyyy-MM-dd/HH:mm"));
    // parse the offset
    ZoneOffset zoneOffset = ZoneOffset.of(offset);
    // create an OffsetDateTime using the parsed offset
    OffsetDateTime odt = OffsetDateTime.of(ldt, zoneOffset);
    // print the date time with the parsed offset
    System.out.println(zoneOffset.toString()
            + ":\t" + odt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
    // create a ZonedDateTime from the OffsetDateTime and use UTC as time zone
    ZonedDateTime utcZdt = odt.atZoneSameInstant(ZoneOffset.UTC);
    // print the date time in UTC using the ISO ZONED DATE TIME format
    System.out.println("UTC:\t"
            + utcZdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    // and then print it again using your desired format
    System.out.println("UTC:\t"
            + utcZdt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm")));
}

我系统上的输出是:

+07:00: 2019-11-05T23:00:00+07:00
UTC:    2019-11-05T16:00:00Z
UTC:    2019-11-05-16:00

对于相反的情况(你得到一个 UTC 时间和一个偏移量并想要 OffsetDateTime),这可能会起作用:

public static void main(String[] args) {
    // this simulates the parameters passed to your method
    String offset = "+07:00";
    String date = "2019-11-05/16:00";
    // provide a pattern
    String formatPattern = "yyyy-MM-dd/HH:mm";
    // and create a formatter with it
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(formatPattern);
    // then parse the time to a local date using the formatter
    LocalDateTime ldt = LocalDateTime.parse(date, dtf);
    // create a moment in time at the UTC offset (that is just +00:00)
    Instant instant = Instant.ofEpochSecond(ldt.toEpochSecond(ZoneOffset.of("+00:00")));
    // and convert the time to one with the desired offset
    OffsetDateTime zdt = instant.atOffset(ZoneOffset.of(offset));
    // finally print it using your formatter
    System.out.println("UTC:\t" + ldt.format(dtf));
    System.out.println(zdt.getOffset().toString()
            + ": " + zdt.format(DateTimeFormatter.ofPattern(formatPattern)));
}

输出是这样的:

UTC:    2019-11-05/16:00
+07:00: 2019-11-05/23:00

【讨论】:

  • 如果我获得了 UTC 时间和偏移量,我如何将 UTC 转换为具有上述偏移量的本地时区?
  • @LuciferMorningstar 我想出了一个反向的例子,看看它。它并不完美,但应该可以。
猜你喜欢
  • 2017-05-07
  • 2017-07-31
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
  • 2011-03-22
  • 1970-01-01
相关资源
最近更新 更多