【问题标题】:How to get timezone from given datetime string in java?如何从java中的给定日期时间字符串获取时区?
【发布时间】:2019-01-13 12:55:18
【问题描述】:

我想从提供的日期和时间中获取时区作为字符串,并希望将其转换为本地时区日期时间。 我的简单日期格式是

“EEE yyyy-MM-dd 'at' hh:mm:ss aa zzz”

据此,客户的日期即将到来

太平洋标准时间 2018 年 8 月 2 日星期四上午 7:10:19

现在我想将 PST 作为时区并转换本地时区的日期和时间(例如 IST)

我正在使用 JAVA 8。

【问题讨论】:

  • 你应该试试this
  • @user12346352 不完全是重复的。这个问题更笼统,这个问题以 java.util.Date 对象开始(显然,不确定问题的组成很差)。
  • 你基本上不能。 PST 比较模糊,可能是菲律宾标准时间、皮特凯恩标准时间或太平洋标准时间(不知道是否还有更多可能)。

标签: java date datetime java-8 timezone


【解决方案1】:

tl;博士

myJavaUtilDate                     // Some `java.util.Date` object. This troublesome class is obsolete.
.toInstant()                       // Convert from terrible legacy class to modern *java.time* class. Both represent a moment in UTC, always UTC.
.atZone(                           // Adjust from `Instant` in UTC to a `ZonedDateTime` in the wall-clock time used by the people of a particular region (a time zone).
    ZoneId.of( "Asia/Kolkata" )    // Specify time zone with `Continent/Region` name, never 3-4 letter pseudo-zone.
)                                  // Returns a `ZonedDateTime` object.
.toString()                        // Generate a String in standard ISO 8601 format extended to append name of zone.

java.time

所以你手头有一个java.util.Date 对象?从那个可怕的过时类转换为现代替代品java.time.Instant

Instant instant = myJavaUtilDate.toInstant() ;

应用时区 (ZoneId) 以获得 ZonedDateTime

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

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;  
ZonedDateTime zdt = instant.atZone( z ) ;

要生成一个字符串是标准 ISO 8601 格式,明智地扩展为在方括号中附加时区名称,请调用 toString

String output = zdt.toString() ;  // Standard ISO 8601 format, extended by appending name of zone.

对于其他格式,search Stack Overflow for DateTimeFormatter class。这已经讨论过很多次了。


关于java.time

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

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

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

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

从哪里获得 java.time 类?

ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

【讨论】:

    【解决方案2】:

    您可以像这样格式化您的日期:

    Date date = new Date();
    
    DateFormat formatter = new SimpleDateFormat("dd MM yyyy HH:mm:ss z");
    formatter.setTimeZone(TimeZone.getTimeZone("IST"));
    System.out.println(formatter.format(date));
    

    【讨论】:

    • 请不要建议 Java 8 用户使用 DateCalendar 类。它们非常过时,不应该在现代代码中使用。
    • 日期只是一个例子,但据我所知SimpleDateFormat 应该不会过时
    • 是的,因为它只适用于Date。现在应该使用 java.timejava.time.format 包中的类。
    • 我还在 Java 9 中使用过 DateSimpleDateFormat,但我从来没有遇到过任何问题...如果我仍然使用此函数而不是 Java.time.format,会出现哪个问题包
    • 我写了代码'DateFormat formatter = new SimpleDateFormat("EEE yyyy-MM-dd 'at' hh:mm:ss aa zzz");日期 date = formatter.parse("星期四 2018-08-06 at 08:10:19 pm IST"); System.out.println(日期); formatter.setTimeZone(TimeZone.getDefault()); System.out.println(formatter.format(date));'输出是我的默认时区是 EDT 时:Mon Aug 06 14:10:19 EDT 2018 Mon 2018-08-06 at 02:10:19 PM EDT 我猜这是不正确的。你能告诉我为什么吗?我是错过了什么还是做错了什么
    猜你喜欢
    • 2016-05-13
    • 2023-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 2023-03-18
    • 2018-04-09
    • 2015-05-11
    相关资源
    最近更新 更多