如果我正确理解了这个问题,听起来您正试图将一个时区的日期/时间转换为另一个具有相同本地时间和不同时区的日期/时间;也就是说,不同的时间点。
您可以通过将LocalDateTime 与新区域结合使用 Noda Time 来做到这一点。例如,给定如下内容:
Instant now = SystemClock.Instance.Now;
DateTimeZone eastern = DateTimeZoneProviders.Tzdb["America/New_York"];
ZonedDateTime nowEastern = now.InZone(eastern);
nowEastern 是America/New_York 时区的现在时间。如果我们将nowEastern 直接打印到控制台,我们会看到类似2014-02-22T05:18:50 America/New_York (-05) 的内容。
顺便说一句,“EST”和“CST”不是时区:它们是时区内特定偏移量的非唯一缩写; America/New_York 和 America/Chicago 可能代表了我们所认为的“东部”和“中部”(如果你真的想要 EST,即使夏令时生效,你也可以使用类似 UTC-05:00 的东西)。
给定任意时区的ZonedDateTime,我们可以将其转换为具有相同本地时间和指定时区的ZonedDateTime,如下所示:
DateTimeZone central = DateTimeZoneProviders.Tzdb["America/Chicago"];
ZonedDateTime sameLocalTimeCentral = nowEastern.LocalDateTime.InZoneStrictly(central);
这给了我们一个ZonedDateTime,它的当地时间相同,但时区不同。使用上面的输入,结果将是2014-02-22T05:18:50 America/Chicago (-06)。
请注意,我使用的是InZoneStrictly。如果本地时间不明确或无效(例如,在夏令时转换期间),这将 throw an exception。如果这是不可接受的,您可以使用InZoneLeniently,它会在给定的本地时间或之后选择最早的有效ZonedDateTime,或InZone,它允许您在这些情况下指定自己的规则。