Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示ISO-8601日期+;当前时间中的时间?_Java_Timezone_Utc - Fatal编程技术网

Java 显示ISO-8601日期+;当前时间中的时间?

Java 显示ISO-8601日期+;当前时间中的时间?,java,timezone,utc,Java,Timezone,Utc,例如,2012-10-30T22:30:00+0300需要在2012-10-30T22:30:00-0600中显示(例如当地时间) 需要在java中实现(android应用程序) 我怎样才能做到这一点呢?这就是日期的含义:时间上的普遍瞬间。在显示时选择适当的时区,您将获得所需的时间字符串: Date now = new Date(); DateFormat df = df.getDateTimeInstance(); System.out.println(df.format(now)); //

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

这就是日期的含义:时间上的普遍瞬间。在显示时选择适当的时区,您将获得所需的时间字符串:

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

使用joda time library以最佳方式解决了我的问题,使用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());
tl;博士 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”)

通过提取
即时
对象,调整为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 );
ZdtControl.toString():2012-11-05T06:00-05:00[美国/蒙特利尔]

所有这些对象(
odt
instant
zdtLondon
,以及
zdtontreal
)都代表了非常相同的同时时刻,即时间线上的同一点。相同的时刻,不同的挂钟时间



关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

该项目现已启动,建议迁移到类

您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*
类。Hibernate5和JPA2.2支持java.time

从哪里获得java.time类

  • 、和更高版本-标准Java API的一部分,带有捆绑实现。
    • Java9添加了一些次要功能和修复
    • 大多数java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类

    • 对于早期的Android(你能详细说明一下吗?我不明白你想要实现什么。22:30:00+0300与22:30:00-0600的时间不同。确切地说,我会给你一个例子——如果我在德国有活动时间,但我现在在伦敦,我需要以伦敦的时间显示德国活动时间。我知道我可以使用date-DateFormat实现,有没有办法o更笼统地写下来,这样我就不必检查所有时区了?对不起,我误解了你的答案:为了处理未知时区,我取了远程时间并从中获取时区,然后将其与当前时区一起使用。
      ZoneId zMontreal = ZoneId.of( "America/Montreal" );
      ZonedDateTime zdtMontreal = instant.atZone( zMontreal );