Gui*_*íez 2 java date utc java-8 java-time
我正在尝试将巴西本地日期转换为 UTC 格式。我已经开发了我的解决方案,但我相信它可以改进。我一直在寻找其他问题,但没有成功。
我的问题是,当我使用以下方法处理Date对象时:
Instant endDateTime = questionDate.toInstant();
Run Code Online (Sandbox Code Playgroud)
我收到一个 UTC 日期,"2017-11-16T00:00:00Z"但这应该是巴西本地日期(不正确,因为它有尾随"Z"),当我尝试转换为 UTC 时,我收到相同的输出。
另一方面,如果我使用ZoneDateTimeclass 并使用LocalDateTimeobject构建日期,我会在输出中丢失秒数:"2017-11-16T02:00Z". 当我使用时会发生这种情况:
LocalTime.of(hour, minutes, seconds);
Run Code Online (Sandbox Code Playgroud)
我搜索LocalTime课堂,我认为这是因为分钟或秒,0但我不确定。
OffsetDateTime类"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);
}
Run Code Online (Sandbox Code Playgroud)
如果您的输入是 a 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);
Run Code Online (Sandbox Code Playgroud)
这将返回一个 OffsetDateTime 与2017-11-16T02:00Z您期望的值。
请注意, OffsetDateTime 没有格式 - 因此该对象确实知道其秒数设置为 0 但默认toString方法不打印它们。如果要以秒为单位进行打印,可以使用格式化程序:
//Formatting
System.out.println(sameInstantUtc.format(DateTimeFormatter.ISO_INSTANT));
Run Code Online (Sandbox Code Playgroud)
哪个打印 2017-11-16T02:00:00Z
如果您的输入是java.sql.Date,则可以使用稍微不同的策略:
LocalDate d = sqlDate.toLocalDate();
ZonedDateTime localInstant = d.atStartOfDay(ZoneOffset.UTC);
Run Code Online (Sandbox Code Playgroud)
其余的代码将是相同的。