gpt4 book ai didi

java - 如何解析日期时间并将其与相应的时区关联

转载 作者:行者123 更新时间:2023-12-01 07:00:20 26 4
gpt4 key购买 nike

我正在尝试读取格式为2019-12-23 13:35:00的字符串以创建DateTime对象,并将其与我知道它对应的特定时区关联(GMT-5)

我正在尝试这样做:

 val string1 = "2019-12-23 13:35:00"
val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
sdf.setTimeZone(StaticDateTimeZone.forID("Etc/GMT-5").toTimeZone)
val date = sdf.parse(string1)


输出不是我想要的,因为它不是假设datetime不在GMT-5时区中,而是在系统的时区(GMT + 0)中,将datetime调整为GMT-5如下: Mon Dec 23 08:35:00 GMT 2019

如何创建一个不会更改输入的datetime对象,而只将其与时区相关联?

最佳答案

(使用Java语法而不是Scala)

tl; dr


  我知道它对应的特定时区(GMT-5)


GMT-5不是时区,仅是偏移量。时区具有Continent/Region格式的名称,例如Africa/Tunis。时区是区域使用的偏移量变化的历史记录。

您无法根据偏移量可靠地确定时区。许多时区在特定时刻可能共享相同的偏移量,但在其他时刻可能有所不同。因此,您只会做出疯狂的猜测。仅在确定地知道特定时区时才应用时区。

而是使用偏移量本身来确定不带区域的时刻。这是一线解决方案。

LocalDateTime                                     // Represent a date with time-of-day but lacking the context of an offset-from-UTC or a time zone.
.parse( // When parsing, no need to specify a formatting pattern when the input complies with the ISO 8601 standard.
"2019-12-23 13:35:00".replace( " " , "T" ) // Comply with ISO 8601 standard formatting, replacing SPACE in the middle with an uppercase `T`.
) // Returns a `LocalDateTime` object.
.atOffset( // Determine a moment by placing the date and time-of-day of a `LocalDateTime` into the context of an offset-from-UTC.
ZoneOffset.ofHours( -5 ) // Represent an offset-from-UTC with `ZoneOffset`, merely a number of hours-minutes-seconds ahead or behind the prime meridian.
) // Returns a `OffsetDateTime` object.
.toString() // Generate text in standard ISO 8601 format.


Run code live


  2019-12-23T13:35-05:00


java.time

ISO 8601

您的输入字符串几乎采用ISO 8601标准格式。为了符合要求,请用 T替换中间的空格。

String input = "2019-12-23 13:35:00".replace( " " , "T" ) ;


LocalDateTime

给定输入缺少时区或UTC偏移量,将其解析为 LocalDateTime

LocalDateTime ldt = LocalDateTime.parse( input ) ;


请注意, LocalDateTime不是时刻,不是时间轴上的一点。在您大约下午1:30的示例中,我们不知道这是指日本东京的1:30 PM还是美国的俄亥俄州托莱多的1:30 PM,这两个小时相差很远。

OffsetDateTime

您声称肯定知道要通过UTC(通常在本初子午线以西)之后5个小时的偏移来查看此日期和时间。

ZoneOffset offset = ZoneOffset.ofHours( -5 ) ;  // Five hours before UTC.


因此,让我们应用该 ZoneOffset以获得一个 OffsetDateTime

OffsetDateTime odt = ldt.atOffset( offset ) ;


参见此 code run live at IdeOne.com


  odt.toString():2019-12-23T13:35-05:00


ZonedDateTime

时区总是比仅从UTC偏移更好。偏移量是小时-分钟-秒,仅此而已。时区更多。时区是特定区域的人们过去,现在和将来对偏移量的更改的历史记录。

但是不要在某个时区猜测。仅当您知道 LocalDateTime对象最初用于该区域时,才应用区域。

Continent/Region的格式指定 proper time zone name,例如 America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用2-4个字母的缩写,例如 ESTIST,因为它们不是真实的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Panama" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;




Table of date-time types in Java, both modern and legacy



关于java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧 legacy日期时间类,例如 java.util.DateCalendarSimpleDateFormat

要了解更多信息,请参见 Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。规格为 JSR 310

现在位于 Joda-Time中的 maintenance mode项目建议迁移到 java.time类。

您可以直接与数据库交换java.time对象。使用与 JDBC driver或更高版本兼容的 JDBC 4.2。不需要字符串,不需要 java.sql.*类。

在哪里获取java.time类?


Java SE 8Java SE 9Java SE 10Java SE 11和更高版本-具有捆绑实现的标准Java API的一部分。


Java 9添加了一些次要功能和修复。

Java SE 6Java SE 7


大多数java.time功能都被反向移植到 ThreeTen-Backport中的Java 6和7。

Android


更高版本的Android捆绑了java.time类的实现。
对于较早的Android(<26), ThreeTenABP项目改编为 ThreeTen-Backport(如上所述)。请参见 How to use ThreeTenABP…



ThreeTen-Extra项目使用其他类扩展了java.time。该项目是将来可能向java.time添加内容的试验场。您可能会在这里找到一些有用的类,例如 IntervalYearWeekYearQuartermore

关于java - 如何解析日期时间并将其与相应的时区关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59473083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com