简体   繁体   English

如何使用Java 8库将UTC日期时间转换为另一个时区?

[英]How to convert UTC DateTime to another Time Zone using Java 8 library?

final Timestamp rawDateTime = Timestamp.valueOf("2031-04-25 18:30:00");
final ZoneId zoneId = ZoneId.of("Asia/Calcutta");
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(
    Instant.ofEpochMilli(rawDateTime.getTime()), zoneId); 
// here we are getting output as 2031-04-25T18:30+05:30[Asia/Calcutta]

final ZonedDateTime zonedDateTime1 = 
ZonedDateTime.of(rawDateTime.toLocalDateTime(), zoneId);
// here we are getting output as 2031-04-25T18:30+05:30[Asia/Calcutta]

But I want to get the converted date time as 2031-04-26 00:00:00+5:30 as my timestamp value is in the UTC Timezone. 但我想将转换后的日期时间设为2031-04-26 00:00:00 + 5:30,因为我的时间戳记值位于UTC时区。

Please help. 请帮忙。

First, you should not use Timestamp . 首先,您不应该使用Timestamp You can use DateTimeFormatter to parse into a LocalDateTime . 您可以使用DateTimeFormatter解析为LocalDateTime

You then zone that LocalDateTime to UTC before converting to the Calcutta zone with ZonedDateTime.withZoneSameInstant . 然后,您可以使用ZonedDateTime.withZoneSameInstant将该LocalDateTime区域ZonedDateTime.withZoneSameInstant为UTC,然后再转换为Calcutta区域。

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
    .append(DateTimeFormatter.ISO_LOCAL_DATE)
    .appendLiteral(' ')
    .append(DateTimeFormatter.ISO_LOCAL_TIME)
    .toFormatter();

LocalDateTime localDateTime = LocalDateTime.parse("2031-04-25 18:30:00", formatter);
ZoneId calcuttaZone = ZoneId.of("Asia/Calcutta");
ZonedDateTime calcuttaZonedDateTime = localDateTime.atZone(ZoneOffset.UTC)
    .withZoneSameInstant(calcuttaZone);

Instead of ZonedDateTime with named zones having (supra-)national standards like day-time-savings, use OffsetDateTime. 而不是使用具有(超)国家标准(例如,节省时间)的命名区域的ZonedDateTime,请使用OffsetDateTime。

OffsetDateTime utc = OffsetDateTime.parse("2031-04-25T18:30:00Z");
OffsetDateTime asia = utc.withOffsetSameInstant(ZoneOffset.ofHoursMinutes(5, 30));

The default parsing is for the ISO format. 默认解析是针对ISO格式的。

  • Z means zero, UTC, +0:00. Z表示零,UTC,+ 0:00。
  • The resulting default formatting is 2031-04-26T00:00+05:30 . 结果默认格式为2031-04-26T00:00+05:30

After comment of Ole VV 经过Ole VV评论

The above is especially error prone if summer time is involved, like in Central European Time with varying offsets +1:00 and +2:00. 如果涉及夏季时间,则上述情况尤其容易出错,例如中欧时间的偏移+1:00和+2:00。

Instant raw = Instant.parse("2031-04-25T18:30:00Z");
ZonedDateTime zoned = raw.atZone(ZoneId.of("Asia/Calcutta"));
OffsetDateTime offset = OffsetDateTime.from(zoned);

Using DateTimeFormatter to format ZonedDateTime: 使用DateTimeFormatter格式化ZonedDateTime:

    final Timestamp rawDateTime = Timestamp.valueOf("2031-04-25 18:30:00");
    final ZoneId zoneId = ZoneId.of("Asia/Calcutta");
    final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(
            Instant.ofEpochMilli(rawDateTime.getTime()), zoneId);
    // here we are getting output as 2031-04-25T18:30+05:30[Asia/Calcutta]

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[XXX]");
    System.out.println(formatter.format(zonedDateTime));

    final ZonedDateTime zonedDateTime1 =
            ZonedDateTime.of(rawDateTime.toLocalDateTime(), zoneId);
    // here we are getting output as 2031-04-25T18:30+05:30[Asia/Calcutta]
    System.out.println(formatter.format(zonedDateTime1));

The output: 输出:

2031-04-25 23:00:00+05:30
2031-04-25 18:30:00+05:30

Edited: according to the comment from @Ole VV - The local date time has to be converted to the zonedatetime , before applying the format : 编辑:根据@Ole VV的评论-在应用格式之前,必须将本地日期时间转换为zonedatetime:

 final Timestamp rawDateTime = Timestamp.valueOf("2031-04-25 18:30:00");
        LocalDateTime ldt = rawDateTime.toLocalDateTime();
        final ZoneId zoneId = ZoneId.of("Asia/Calcutta");
        ZonedDateTime zdt = ldt.atZone(ZoneId.of("UTC"))
                .withZoneSameInstant(zoneId);


        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[XXX]");
        System.out.println(formatter.format(zdt));

This will give the output: 这将给出输出:

2031-04-26 00:00:00+5:30

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

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