Java 8 UTC到本地方法,位于Joda Time to Java 8的DateTimeZone中
我们正在将Joda时间API更改为Java 8时间API。在Joda时代,我使用了:Java 8 UTC到本地方法,位于Joda Time to Java 8的DateTimeZone中,java-8,jodatime,java-time,Java 8,Jodatime,Java Time,我们正在将Joda时间API更改为Java 8时间API。在Joda时代,我使用了: DateTimeZone.convertLocalToUTC(this.getMillis(), true); DateTimeZone.convertUTCToLocal(long millis); 有谁能告诉我Java8中的等效方法吗 已编辑 convertLocalToUTC DateTimeZone dateTimeZone = DateTimeZone.getDefault(); DateTime
DateTimeZone.convertLocalToUTC(this.getMillis(), true);
DateTimeZone.convertUTCToLocal(long millis);
有谁能告诉我Java8中的等效方法吗
已编辑
convertLocalToUTC
DateTimeZone dateTimeZone = DateTimeZone.getDefault();
DateTime jodadatetime = new DateTime();
long utcTime = dateTimeZone.convertLocalToUTC(jodadatetime .getMillis(), true);
System.out.println(jodadatetime);
DateTimeZone dateTimeZone1 = DateTimeZone.UTC;
System.out.println(new DateTime(utcTime, dateTimeZone1));
输出
2017-08-09T17:27:57.508+05:302017-08-09T06:27:57.508Z ConvertUtcToLocal
long utctolocal = dateTimeZone.convertUTCToLocal(jodadatetime.getMillis());
System.out.println("utc to local : " + new DateTime(utctolocal, dateTimeZone1));
输出
2017-08-09T17:27:57.508Z
以下是一些可能有助于您入门的代码片段:
LocalDateTime yourLocalTime = ...
long utc = yourLocalTime.toInstant(ZoneOffset.UTC).toEpochMilli();
或者使用strict=true
以下命令:
long utc = ZonedDateTime.ofStrict(yourLocalTime, ZoneOffset.UTC, ZoneId.of("Z")).toInstant().toEpochMilli();
转换回与此类似:
LocalDateTime yourUtcTime = ...
long localTimeInMillis = yourUtcTime.toInstant(OffsetDateTime.now().getOffset() /* or: yourLocalTime.getOffset() */).toEpochMilli();
ZoneId zone = ZoneId.of("Asia/Calcutta");
// original date 2017-08-09T17:27:57.508+05:30
ZonedDateTime z = OffsetDateTime.parse("2017-08-09T17:27:57.508+05:30")
// convert to a ZonedDateTime in Calcutta (2017-08-09T17:27:57.508+05:30[Asia/Calcutta])
.atZoneSameInstant(zone)
// convert to same local time in UTC
.withZoneSameLocal(ZoneOffset.UTC);
System.out.println(z.toInstant().toEpochMilli() + "=" + z);
如果您不需要Milli,但希望使用DateTime类,您可能希望使用ZonedDateTime
从millis创建LocalDateTime
的操作如下:
LocalDateTime yourLocalDateTime = Instant.ofEpochMilli(millisAsLong).atZone(/* your desired zone here */).toLocalDateTime();
如果区域id可以是
ZoneId.of(“Z”)
,ZoneId.systemDefault()
,等等,那么您的原始日期是2017-08-09T17:27:57.508+05:30
,那么您需要两件事:
convertLocalToUTC
:获取2017-08-09T06:27:57.508Z
。这有点棘手:2017-08-09T17:27:57.508+05:30
,在UTC中相当于2017-08-09T11:57:57.508Z
。此方法的作用是将其转换为相同的本地日期和时间,但在加尔各答时区,然后用UTC打印。简言之:
- 原始日期为2017-08-09T17:27:57.508+05:30
- 在UTC中,这与
2017-08-09T11:57:57.508Z
- 在UTC中,这与
将其转换为convertLocalToUTC
(相同的日期和时间,但在加尔各答时区)2017-08-09T11:57:57.508+05:30
- 这与
2017-08-09T06:27:57.508Z
- 这与
ZoneId zone = ZoneId.of("Asia/Calcutta");
// original date 2017-08-09T17:27:57.508+05:30
Instant i = OffsetDateTime.parse("2017-08-09T17:27:57.508+05:30")
// convert to UTC (2017-08-09T11:57:57.508Z)
.atZoneSameInstant(ZoneOffset.UTC)
// convert to same local time in Calcutta
.withZoneSameLocal(zone)
// back to UTC
.toInstant();
System.out.println(i.toEpochMilli() + "=" + i);
输出:
1502260077508=2017-08-09T06:27:57.508Z
convertUTCToLocal
:获取2017-08-09T17:27:57.508Z
-相同的日期(2017-08-09)和时间(17:27:57.508),但使用UTCLocalDateTime yourUtcTime = ...
long localTimeInMillis = yourUtcTime.toInstant(OffsetDateTime.now().getOffset() /* or: yourLocalTime.getOffset() */).toEpochMilli();
ZoneId zone = ZoneId.of("Asia/Calcutta");
// original date 2017-08-09T17:27:57.508+05:30
ZonedDateTime z = OffsetDateTime.parse("2017-08-09T17:27:57.508+05:30")
// convert to a ZonedDateTime in Calcutta (2017-08-09T17:27:57.508+05:30[Asia/Calcutta])
.atZoneSameInstant(zone)
// convert to same local time in UTC
.withZoneSameLocal(ZoneOffset.UTC);
System.out.println(z.toInstant().toEpochMilli() + "=" + z);
输出:
1502299677508=2017-08-09T17:27:57.508Z
您还可以从毫秒值中获取日期 对于案例1:
// millis for original joda date: jodadatetime.getMillis() (1502279877508 = 2017-08-09T17:27:57.508+05:30)
long millisFromJoda = 1502279877508L;
Instant instant = Instant.ofEpochMilli(millisFromJoda)
// convert to UTC (2017-08-09T11:57:57.508Z)
.atZone(ZoneOffset.UTC)
// convert to same local time in Calcutta
.withZoneSameLocal(zone)
// back to UTC
.toInstant();
System.out.println(instant.toEpochMilli() + "=" + instant);
输出:
1502260077508=2017-08-09T06:27:57.508Z
如果需要,您可以将即时
转换为其他类型:
// convert to ZonedDateTime in UTC
ZonedDateTime zd = instant.atZone(ZoneOffset.UTC);
// convert to OffsetDateTime in UTC
OffsetDateTime odt = instant.atOffset(ZoneOffset.UTC);
两者都将是2017-08-09T06:27:57.508Z
对于案例2:
ZonedDateTime zdt = Instant.ofEpochMilli(millisFromJoda)
// convert to a ZonedDateTime in Calcutta (2017-08-09T17:27:57.508+05:30[Asia/Calcutta])
.atZone(zone)
// convert to same local time in UTC
.withZoneSameLocal(ZoneOffset.UTC);
System.out.println(zdt.toInstant().toEpochMilli() + "=" + zdt);
输出:
1502299677508=2017-08-09T17:27:57.508Z
convertUTCToLocal
将long
作为参数,而不是DateTimeZone
。还是我遗漏了什么?@Hugo抱歉我弄错了默认时区是什么?@Hugo[Asia/Calcutta]在我的代码LocaltoUtc转换中给出2017-08-09T06:50:15.644Z,但我使用了你的代码ZoneDateTime.ofStrict(你的LocalTime,ZoneOffset.UTC,ZoneId.of(“Z”))它给出了2017-08-09T17:50:15.582Z对于带有毫秒的情况1,我想要ZonedDatetime/OffsetDateTime,而不是instant。当我尝试将instant更改为Zoned/OffsetDateTime时,时间正在更改