我们正在将Joda-Time API更改为Java 8时间API。在Joda-Time我用过:
react/addons
任何人都能告诉我Java 8中的等效方法吗?
已修改
convertLocalToUTC
DateTimeZone.convertLocalToUTC(this.getMillis(), true);
DateTimeZone.convertUTCToLocal(long millis);
输出
2017-08-09T17:27:57.508 + 05:30个
2017-08-09T06:27:57.508Z
ConvertUtcToLocal
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.508Z
答案 0 :(得分:3)
因此,您的原始日期为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
2017-08-09T11:57:57.508Z
convertLocalToUTC
将此转换为2017-08-09T11:57:57.508+05:30
(相同的日期和时间,但在加尔各答时区)
2017-08-09T06:27:57.508Z
要在Java 8中执行此操作,您可以执行以下操作:
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),但以UTC为准。类似:
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
您还可以从millis值中获取日期。
案例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
如果需要,您可以将Instant
转换为其他类型:
// 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
答案 1 :(得分:0)
以下是一些可以帮助您入门的代码段:
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();
如果您不需要millis,但想要使用DateTime-classes,您可能想要使用ZonedDateTime
代替?
从millis创建LocalDateTime
可以按如下方式完成:
LocalDateTime yourLocalDateTime = Instant.ofEpochMilli(millisAsLong).atZone(/* your desired zone here */).toLocalDateTime();
其中,区域ID可以是ZoneId.of("Z")
,ZoneId.systemDefault()
等。