使用offset将一个时区中的datetime字符串转换为另一个时区

时间:2021-07-11 12:01:50

I have a datetime string "2018-01-15 01:16:00" which is in EST timezone. I want to convert this into another timezone dynamically using the UTC offset. My javascript code passes this UTC offset as a parameter and the servlet has to convert/format this datetime string to the timezone identified by the provided offset.

我有一个日期时间字符串“2018-01-15 01:16:00”,它位于EST时区。我想使用UTC偏移动态地将其转换为另一个时区。我的javascript代码将此UTC偏移量作为参数传递,并且servlet必须将此日期时间字符串转换/格式化为由提供的偏移量标识的时区。

I have tried many approaches including the one documented in the oracle tutorials but unable to arrive at a solution.

我尝试了很多方法,包括oracle教程中记录的方法,但无法找到解决方案。

Below is my code that I am trying, any help is greatly appreciated.

以下是我正在尝试的代码,非常感谢任何帮助。

private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String DEFAULT_TIME_ZONE = ZoneId.SHORT_IDS.get("EST");

public static void main(String[] args) throws Exception {
    String dateTime = "2018-01-15 02:35:00";
    //parse the datetime using LocalDateTime
    LocalDateTime defaultDateTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(DATE_FORMAT));

    //get the datetime in default timezone
    ZoneId defaultZoneId = ZoneId.of(DEFAULT_TIME_ZONE);
    ZonedDateTime defaultZoneDateTime = defaultDateTime.atZone(defaultZoneId);
    System.out.println("EST time: "+defaultZoneDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT)));
    ZonedDateTime utcZonedDateTime = defaultZoneDateTime.withZoneSameInstant(ZoneId.of("UTC"));
    String utcTime = defaultZoneDateTime.withZoneSameInstant(ZoneId.of("UTC")).format(DateTimeFormatter.ofPattern(DATE_FORMAT));
    System.out.println("UTC : "+utcTime);

    //IST timezone
    ZoneOffset offset = ZoneOffset.of("+05:30");
    OffsetDateTime offsetDate = OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset);
    String targetTimeZone = offsetDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));
    System.out.printf("target time : "+targetTimeZone);

}

OUTPUT

OUTPUT

EST time: 2018-01-15 02:35:00
UTC : 2018-01-15 07:37:00
target time : 2018-01-15 07:37:00

Expected target time : 2018-01-15 13:05:00

预计目标时间:2018-01-15 13:05:00

2 个解决方案

#1


13  

The immediate problem is this line:

直接问题是这一行:

OffsetDateTime offsetDate = OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset);

That's saying you want the same local date/time, but with the specified offset. That changes which instant in time is being represented.

这就是说你想要相同的本地日期/时间,但是要有指定的偏移量。这改变了即时代表的时间。

Instead, you really want to represent the same instant in time, but at a particular offset. So the shortest fix is:

相反,你真的想要及时代表同一时刻,但是在特定的偏移处。所以最短的修复是:

OffsetDateTime offsetDate = utcZonedDateTime.toInstant().atOffset(offset);

However, there are a number of other aspects which could do with changing:

但是,还有许多其他方面可以改变:

  • Prefer ZoneOffset.UTC to ZoneId.of("UTC")
  • 首选ZoneOffset.UTC到ZoneId.of(“UTC”)
  • Using EST as a time zone is confusing - it's not clear whether you expect it to mean "Eastern Time" (changing between EST and EDT) or pure standard time of UTC-5. Assuming you actually mean "Eastern Time" it would be better to use America/New_York as a zone ID.
  • 使用EST作为时区令人困惑 - 目前尚不清楚您是否期望它意味着“东部时间”(在EST和EDT之间变化)或纯标准时间UTC-5。假设你实际上是指“东部时间”,那么最好使用America / New_York作为区域ID。
  • It's unclear what you want to happen if the input string represents a skipped or ambiguous value in Eastern time. These happen around DST transitions.
  • 如果输入字符串表示东部时间的跳过或模糊值,则不清楚您想要发生什么。这些发生在DST转换周围。

Next, you don't need to convert the ZonedDateTime in Eastern time into a ZonedDateTime in UTC at all. Either convert it directly to an instant:

接下来,您根本不需要将东部时间的ZonedDateTime转换为UTC中的ZonedDateTime。将其直接转换为瞬间:

OffsetDateTime target = defaultZoneDateTime.toInstant().at(offset);

Or create a ZonedDateTime for the target instead:

或者为目标创建ZonedDateTime:

ZonedDateTime target = defaultZoneDateTime.withZoneSameInstant(offset);

Given that an offset isn't really a time zone, I'd probably go with the first of these.

鉴于偏移不是真正的时区,我可能会选择其中的第一个。

#2


5  

You're using

你正在使用

OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset)

to create your target. You're thus constructing an OffsetDateTime in the target offset, having a LocalDateTime equal to the LocalDateTime in the UTC zone.

创建目标。因此,您在目标偏移量中构造一个OffsetDateTime,其LocalDateTime等于UTC区域中的LocalDateTime。

What you want is the exact same transformation as the one you're using to go from the EST time to UTC: keep the same instant, but go to a different timezone:

您想要的是与您从EST时间到UTC时使用的完全相同的转换:保持相同的瞬间,但转到不同的时区:

defaultZoneDateTime.withZoneSameInstant(offset);

or, if you really want an OffsetDateTime and not a ZonedDateTime:

或者,如果你真的想要一个OffsetDateTime而不是ZonedDateTime:

OffsetDateTime.ofInstant(defaultZoneDateTime.toInstant(), offset);

#1


13  

The immediate problem is this line:

直接问题是这一行:

OffsetDateTime offsetDate = OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset);

That's saying you want the same local date/time, but with the specified offset. That changes which instant in time is being represented.

这就是说你想要相同的本地日期/时间,但是要有指定的偏移量。这改变了即时代表的时间。

Instead, you really want to represent the same instant in time, but at a particular offset. So the shortest fix is:

相反,你真的想要及时代表同一时刻,但是在特定的偏移处。所以最短的修复是:

OffsetDateTime offsetDate = utcZonedDateTime.toInstant().atOffset(offset);

However, there are a number of other aspects which could do with changing:

但是,还有许多其他方面可以改变:

  • Prefer ZoneOffset.UTC to ZoneId.of("UTC")
  • 首选ZoneOffset.UTC到ZoneId.of(“UTC”)
  • Using EST as a time zone is confusing - it's not clear whether you expect it to mean "Eastern Time" (changing between EST and EDT) or pure standard time of UTC-5. Assuming you actually mean "Eastern Time" it would be better to use America/New_York as a zone ID.
  • 使用EST作为时区令人困惑 - 目前尚不清楚您是否期望它意味着“东部时间”(在EST和EDT之间变化)或纯标准时间UTC-5。假设你实际上是指“东部时间”,那么最好使用America / New_York作为区域ID。
  • It's unclear what you want to happen if the input string represents a skipped or ambiguous value in Eastern time. These happen around DST transitions.
  • 如果输入字符串表示东部时间的跳过或模糊值,则不清楚您想要发生什么。这些发生在DST转换周围。

Next, you don't need to convert the ZonedDateTime in Eastern time into a ZonedDateTime in UTC at all. Either convert it directly to an instant:

接下来,您根本不需要将东部时间的ZonedDateTime转换为UTC中的ZonedDateTime。将其直接转换为瞬间:

OffsetDateTime target = defaultZoneDateTime.toInstant().at(offset);

Or create a ZonedDateTime for the target instead:

或者为目标创建ZonedDateTime:

ZonedDateTime target = defaultZoneDateTime.withZoneSameInstant(offset);

Given that an offset isn't really a time zone, I'd probably go with the first of these.

鉴于偏移不是真正的时区,我可能会选择其中的第一个。

#2


5  

You're using

你正在使用

OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset)

to create your target. You're thus constructing an OffsetDateTime in the target offset, having a LocalDateTime equal to the LocalDateTime in the UTC zone.

创建目标。因此,您在目标偏移量中构造一个OffsetDateTime,其LocalDateTime等于UTC区域中的LocalDateTime。

What you want is the exact same transformation as the one you're using to go from the EST time to UTC: keep the same instant, but go to a different timezone:

您想要的是与您从EST时间到UTC时使用的完全相同的转换:保持相同的瞬间,但转到不同的时区:

defaultZoneDateTime.withZoneSameInstant(offset);

or, if you really want an OffsetDateTime and not a ZonedDateTime:

或者,如果你真的想要一个OffsetDateTime而不是ZonedDateTime:

OffsetDateTime.ofInstant(defaultZoneDateTime.toInstant(), offset);