将带有 zoneId 的字符串 localdate 转换为 UTC 偏移日期 JAVA
Convert string local date with zoneId to UTC Offsetdate JAVA
我正在尝试将巴西本地日期转换为 UTC 格式。我已经开发了我的解决方案,但我确信它可以改进。我一直在寻找其他问题,但没有成功。
我的问题是当我处理 Date
对象时:
Instant endDateTime = questionDate.toInstant();
我收到的 UTC 日期为 "2017-11-16T00:00:00Z"
,但这应该是巴西本地日期(不正确,因为它有尾随 "Z"
),当我尝试转换为 UTC 时,我收到相同的输出。
另一方面,如果我使用 ZoneDateTime
class 并使用 LocalDateTime
对象构建日期,我会在输出中丢失秒数:"2017-11-16T02:00Z"
。当我使用时会发生这种情况:
LocalTime.of(hour, minutes, seconds);
我搜索 LocalTime
class 我认为这是因为分钟或秒是 0
但我不确定。
解决的问题:
- 回复还没秒
- 希望Java8有一套功能让这个更简单明了
前提条件:
- 我无法使用 Joda 库
- 结果必须是
OffsetDateTime
class
- 输入:日期
"2017-11-16"
- 输出:
"2017-11-16T02:00:00Z"
这是我的解决方案:
private static OffsetDateTime processDate(Date questionDate) {
Instant endDateTime = questionDate.toInstant();
ZoneId zoneId = ZoneId.of(ZONEID);
String [] date = endDateTime.toString().split("T");
LocalDateTime localDateTime = convertLocalTimeToUtc(date);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
ZonedDateTime utcDate = zonedDateTime.withZoneSameInstant(ZoneOffset.UTC);
return utcDate.toOffsetDateTime();
}
private static LocalDateTime convertLocalTimeToUtc(String[] dateFromCountry) {
LocalDate date = processDate(dateFromCountry[0]);
LocalTime time = processTime(dateFromCountry[1]);
return LocalDateTime.of(date, time);
}
private static LocalDate processDate(String dateFromCountry) {
String [] partsOfDate = dateFromCountry.split("-");
int year = Integer.parseInt(partsOfDate[0]);
int month = Integer.parseInt(partsOfDate[1]);
int day = Integer.parseInt(partsOfDate[2]);
return LocalDate.of(year, month, day);
}
private static LocalTime processTime(String dateFromCountry) {
String [] partsOfTime = dateFromCountry.split(":");
int hour = Integer.parseInt(partsOfTime[0]);
int minutes = Integer.parseInt(partsOfTime[1]);
int seconds = Integer.parseInt(partsOfTime[2].substring(0,1));
return LocalTime.of(hour,minutes,seconds);
}
如果您的输入是 java.util.Date
,您可以摆脱所有的字符串操作:
//simulate your input
Instant input = Instant.parse("2017-11-16T00:00:00Z");
Date d = Date.from(input);
//transformation code starts here
Instant instant = d.toInstant();
ZonedDateTime localInstant = instant.atZone(ZoneOffset.UTC);
ZonedDateTime sameLocalInBrazil = utcInstant.withZoneSameLocal(ZoneId.of("Brazil/East"));
OffsetDateTime sameInstantUtc = sameLocalInBrazil.toOffsetDateTime()
.withOffsetSameInstant(ZoneOffset.UTC);
这 return 一个 OffsetDateTime,如您所料,其值为 2017-11-16T02:00Z
。
请注意,OffsetDateTime 没有格式 - 因此该对象确实知道它的秒数设置为 0,但默认的 toString
方法不会打印它们。如果你想用秒打印它,你可以使用格式化程序:
//Formatting
System.out.println(sameInstantUtc.format(DateTimeFormatter.ISO_INSTANT));
输出 2017-11-16T02:00:00Z
如果您的输入是 java.sql.Date
,您可以使用稍微不同的策略:
LocalDate d = sqlDate.toLocalDate();
ZonedDateTime localInstant = d.atStartOfDay(ZoneOffset.UTC);
其余代码相同。
我正在尝试将巴西本地日期转换为 UTC 格式。我已经开发了我的解决方案,但我确信它可以改进。我一直在寻找其他问题,但没有成功。
我的问题是当我处理 Date
对象时:
Instant endDateTime = questionDate.toInstant();
我收到的 UTC 日期为 "2017-11-16T00:00:00Z"
,但这应该是巴西本地日期(不正确,因为它有尾随 "Z"
),当我尝试转换为 UTC 时,我收到相同的输出。
另一方面,如果我使用 ZoneDateTime
class 并使用 LocalDateTime
对象构建日期,我会在输出中丢失秒数:"2017-11-16T02:00Z"
。当我使用时会发生这种情况:
LocalTime.of(hour, minutes, seconds);
我搜索 LocalTime
class 我认为这是因为分钟或秒是 0
但我不确定。
解决的问题:
- 回复还没秒
- 希望Java8有一套功能让这个更简单明了
前提条件:
- 我无法使用 Joda 库
- 结果必须是
OffsetDateTime
class - 输入:日期
"2017-11-16"
- 输出:
"2017-11-16T02:00:00Z"
这是我的解决方案:
private static OffsetDateTime processDate(Date questionDate) {
Instant endDateTime = questionDate.toInstant();
ZoneId zoneId = ZoneId.of(ZONEID);
String [] date = endDateTime.toString().split("T");
LocalDateTime localDateTime = convertLocalTimeToUtc(date);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
ZonedDateTime utcDate = zonedDateTime.withZoneSameInstant(ZoneOffset.UTC);
return utcDate.toOffsetDateTime();
}
private static LocalDateTime convertLocalTimeToUtc(String[] dateFromCountry) {
LocalDate date = processDate(dateFromCountry[0]);
LocalTime time = processTime(dateFromCountry[1]);
return LocalDateTime.of(date, time);
}
private static LocalDate processDate(String dateFromCountry) {
String [] partsOfDate = dateFromCountry.split("-");
int year = Integer.parseInt(partsOfDate[0]);
int month = Integer.parseInt(partsOfDate[1]);
int day = Integer.parseInt(partsOfDate[2]);
return LocalDate.of(year, month, day);
}
private static LocalTime processTime(String dateFromCountry) {
String [] partsOfTime = dateFromCountry.split(":");
int hour = Integer.parseInt(partsOfTime[0]);
int minutes = Integer.parseInt(partsOfTime[1]);
int seconds = Integer.parseInt(partsOfTime[2].substring(0,1));
return LocalTime.of(hour,minutes,seconds);
}
如果您的输入是 java.util.Date
,您可以摆脱所有的字符串操作:
//simulate your input
Instant input = Instant.parse("2017-11-16T00:00:00Z");
Date d = Date.from(input);
//transformation code starts here
Instant instant = d.toInstant();
ZonedDateTime localInstant = instant.atZone(ZoneOffset.UTC);
ZonedDateTime sameLocalInBrazil = utcInstant.withZoneSameLocal(ZoneId.of("Brazil/East"));
OffsetDateTime sameInstantUtc = sameLocalInBrazil.toOffsetDateTime()
.withOffsetSameInstant(ZoneOffset.UTC);
这 return 一个 OffsetDateTime,如您所料,其值为 2017-11-16T02:00Z
。
请注意,OffsetDateTime 没有格式 - 因此该对象确实知道它的秒数设置为 0,但默认的 toString
方法不会打印它们。如果你想用秒打印它,你可以使用格式化程序:
//Formatting
System.out.println(sameInstantUtc.format(DateTimeFormatter.ISO_INSTANT));
输出 2017-11-16T02:00:00Z
如果您的输入是 java.sql.Date
,您可以使用稍微不同的策略:
LocalDate d = sqlDate.toLocalDate();
ZonedDateTime localInstant = d.atStartOfDay(ZoneOffset.UTC);
其余代码相同。