0

如何将 JavaCalendar对象从本地时间转换为 UTC?这是我尝试过的:

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.setTime(localDateTime.getTime());
    SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
    String dateAsString = outputFmt.format(calendar.getTime());
    System.out.println(dateAsString)

时间始终以 MT 而非 GMT 显示。我想将 UTC 中的日历存储在数据库中(我不关心格式化或显示时间)。

4

4 回答 4

3

您需要调用setTimeZoneSimpleDateFormat而不是调用Calendar

//getting local time 
Calendar calendar = Calendar.getInstance(Locale.getDefault());

SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
outputFmt.setTimeZone(TimeZone.getTimeZone("GMT")); //set timezone here

//shows calendar time correctly set    
System.out.println(outputFmt.format(calendar.getTime()));
于 2018-06-30T12:48:35.540 回答
3

不要将您的日期时间转换为字符串以存储到 SQL 数据库中。使用日期时间对象。

我假设您要存储到数据类型datetimetimestamp没有时区的数据库列中,并且该列将包含 UTC 值(推荐做法)。在这种情况下,进一步假设您至少使用 Java 8 和至少 JDBC 4.2,将LocalDateTimeUTC 格式的 a 存储到数据库中。

    ZonedDateTime dateTime = ZonedDateTime.of(
            2018, 7, 1, 9, 0, 0, 0, ZoneId.of("America/Denver"));
    LocalDateTime dateTimeInUtc = dateTime.withZoneSameInstant(ZoneOffset.UTC)
            .toLocalDateTime();
    System.out.println("UTC: " + dateTimeInUtc);

    PreparedStatement ps = yourDatabaseConnection.prepareStatement(
            "insert into your_table(date_time) values (?)");
    ps.setObject(1, dateTimeInUtc);
    ps.executeUpdate();

代码示例在 7 月 1 日上午 9 点在丹佛进行转换并打印:

UTC: 2018-07-01T15:00

然后它将打印的 UTC 时间存储到数据库中。注意setObject方法的使用。

您使用的Calendar课​​程早已过时,并且设计总是很糟糕。如果您Calendar从遗留 API 获得了一个您现在无法更改或不想更改的对象,请先将其转换为 an Instant,然后从那里进行进一步的转换:

    Calendar localDateTime = // …;
    LocalDateTime dateTimeInUtc = localDateTime.toInstant()
            .atOffset(ZoneOffset.UTC)
            .toLocalDateTime();

我应该在这里使用 LocalDateTime 吗?

编辑:这取决于您在数据库中使用的数据类型和 JDBC 驱动程序的要求。正如我所说,我假设知道数据库中的值是 UTC,并且您使用的数据类型没有时区,所以数据库知道。如果是这种情况,那么使用带有时区或偏移信息的 Java 类型是没有意义的,因为当您存储它时,它无论如何都会丢失。

有人会说,在这种情况下,您已经在数据库中使用了错误的类型,并建议您使用timestamp with timezone列,这样数据库也知道时间是 UTC。在这种情况下,您当然应该在存储的值中包含偏移信息。例如,PostgreSQL JDBC 驱动程序OffsetDateTime在这种情况下需要一个,这是合乎逻辑的(因为它存储的是 UTC 偏移量,而不是实时时区)。

根据您的 DBMS 的功能,可能还有其他选项。我在底部还包含了几个链接,您可以搜索更多。

你的代码出了什么问题?

您的转换Calendar工作正常,并在 UTC 中为您提供了相同的时间点。问题只是在您尝试将其格式化为 a 时才开始String:当您获取时calendar.getTime(),您将获得一个Date包含相同时间点(仍然是正确的时间点)的对象,但 aDate无法保存时区信息,因此您丢失了信息关于UTC。接下来,您Date使用SimpleDateFormat. ASimpleDateFormat有一个时区,如果您不自己设置,它会使用您的 JVM 的时区设置,这可能是 Mountain Time 的一些变体。因此,您的时间以 MT 打印。

链接

于 2018-06-30T17:57:40.067 回答
0

这是我将正常日期转换为 UTC 的方法。因此,首先将您的 Calendar 对象转换为 Date 对象:

public static String convertToUTC(Date date) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    return dateFormat.format(date);
}
于 2018-06-30T12:50:02.317 回答
0

这是我准备了自己的日历对象并将时区设置为UTC,然后获取日期对象。一旦你得到日期对象,你就可以得到以毫秒为单位的时间,这是自纪元以来的毫秒数。

private static Date internalDate(int year, int month, int day, int hour, int minute, int second) {
  Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
  calendar.set(Calendar.YEAR, year);
  calendar.set(Calendar.MONTH, month - 1);
  calendar.set(Calendar.DAY_OF_MONTH, day);
  calendar.set(Calendar.HOUR_OF_DAY, hour);
  calendar.set(Calendar.MINUTE, minute);
  calendar.set(Calendar.SECOND, second);
 // get the Date Object and now your time is in UTC and you can easily convert it to 
 // local or UTC by getting the milli seconds which is date.gerTime(); 
  return calendar.getTime();
}
于 2021-05-19T12:44:49.957 回答