【问题标题】:Show ISO-8601 date+time in current time?在当前时间显示 ISO-8601 日期+时间?
【发布时间】:2012-10-31 11:41:25
【问题描述】:

例如 2012-10-30T22:30:00+0300 需要显示在 2012-10-30T22:30:00-0600 (以当地时间为例) 需要在java中实现(android app) 我怎样才能做到这一点?

【问题讨论】:

  • 您能详细说明一下吗?我不明白你想要达到什么目的。 22:30:00+0300 与 22:30:00-0600 不同。
  • 没错,我给你举个例子——如果我在德国有活动时间,但我现在在伦敦,我需要以伦敦时间显示德国活动时间。跨度>

标签: java timezone utc


【解决方案1】:

这就是日期:一个普遍的瞬间。显示时选择合适的时区,你就会得到你想要的时间字符串:

Date now = new Date();
DateFormat df = df.getDateTimeInstance();
System.out.println(df.format(now)); // now, displayed in the current time zone (examle: Germany)
df.setTimeZone(theLondonTimeZone);
System.out.println(df.format(now)); // now, displayed in the time zone of London

【讨论】:

  • 我知道我可以使用 date DateFormat 来实现,有什么方法可以更通用地写下来,这样我就不必检查所有时区了吗?
  • 对不起,我误解了您的回答:为了处理未知时区,我使用了远程时间并从中获取时区,然后将其与当前时区一起使用。
【解决方案2】:

tl;博士

OffsetDateTime
.parse( 
    "2012-10-30T22:30:00+0300" , 
    DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ssX" )
)
.toInstant()
.atZone(
    ZoneId.of( "Europe/London" ) 
)
.toString()

2012-10-30T19:30Z[欧洲/伦敦]

java.time

现代解决方案使用 java.time 类。

定义格式化程序以匹配您的输入。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ssX" ) ;

将输入解析为OffsetDateTime

String input = "2012-11-05T13:00:00+0200" ;
OffsetDateTime odt = OffsetDateTime.parse( input , f );

odt.toString(): 2012-11-05T13:00+02:00

提示:始终在偏移的小时和分钟之间包含冒号字符作为分隔符。然后我们可以跳过自定义格式模式:OffsetDateTime.parse( "2012-11-05T13:00+02:00" )

通过提取 Instant 对象来调整到 UTC,即零时分秒的偏移量。

Instant instant = odt.toInstant() ;

在标准 ISO 8601 格式中,末尾的 Z 表示 UTC(零偏移)。发音为“祖鲁语”。

instant.toString(): 2012-11-05T11:00:00Z

调整到伦敦时间。

ZoneId zLondon = ZoneId.of( "Europe/London" ) ;
ZonedDateTime zdtLondon = instant.atZone( zLondon ) ;

zdtLondon.toString(): 2012-11-05T11:00Z[欧洲/伦敦]

调整到另一个时区。

ZoneId zMontreal = ZoneId.of( "America/Montreal" );
ZonedDateTime zdtMontreal = instant.atZone( zMontreal );

zdtMontreal.toString(): 2012-11-05T06:00-05:00[美国/蒙特利尔]

所有这些对象(odtinstantzdtLondonzdtMontreal)都代表同一时刻,时间轴上的同一点。同一时刻,不同的挂钟时间。



关于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 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。 Hibernate 5 和 JPA 2.2 支持 java.time

从哪里获得 java.time 类?

https://i.stack.imgur.com/eKgbN.png 哪个 java.time 库与哪个版本的 Java 或 Android 一起使用的表

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

【讨论】:

    【解决方案3】:

    使用 joda 时间库最佳地解决了我的问题,使用 dateTime 和 dateTime zone 如下:

    DateTimeFormatter parser2 = ISODateTimeFormat.dateTimeNoMillis();
        DateTime dt = new DateTime();
        DateTime dt2 = new DateTime();
        dt = DateTime.parse("2012-11-05T13:00:00+0200");
        System.out.println(dt.toString());
    
        dt2 = DateTime.parse("2012-11-05T21:45:00-08:00");
        DateTimeZone dtz = dt2.getZone();
        System.out.println(dt.withZone(dtz).toString());
    

    【讨论】:

      猜你喜欢
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多