将带有 zoneId 的字符串本地日期转换为 UTC Offsetdate JAVA

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但我不确定。

解决方案的问题:

  1. 响应没有秒
  2. 我希望 Java 8 有一组功能可以让这更简单明了

前提:

  • 我不能使用 Joda 库
  • 结果必须是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)

ass*_*ias 5

如果您的输入是 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)

其余的代码将是相同的。