【问题标题】:how to Convert UTC timestamp to some other zone timestamp and extract day,daynumber,date,month,year,time,timezone in Java [closed]如何将 UTC 时间戳转换为其他区域时间戳并在 Java 中提取日、日数、日期、月、年、时间、时区 [关闭]
【发布时间】:2021-09-22 08:15:24
【问题描述】:

我在 json 对象中收到 UTC 时间戳“2021-02-11T09:00:00-07:00 --UTC”,该对象必须转换为其他区域时间戳。一旦转换为所需的区域时间戳,我需要提取日期、天数、 日期、月份、年份、时间、时区并将其存储在地图中。

请多多指教。

【问题讨论】:

  • 使用docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html 类,它具有您需要转换为不同时区并获取日期和月份等所有方法,然后使用您喜欢的任何合适的地图来存储数据
  • 查看来自java.time, the modern Java date and time APIOffsetDateTimeZoneIdZonedDateTime。这应该不会太难。尝试一下,如果不顺利,请就您遇到的任何问题提出一个新问题。
  • 字符串中的日期和时间位于 UTC 偏移量 -07:00。尾随的 `--UTC` 让我感到困惑。它真的是字符串的一部分吗?你知道这是否意味着什么吗?
  • @OleV.V. - 我认为它没有任何意义,因为信息正在通过多个组件传递,它只是为了指示时区。感谢您的意见。当我通过其他解决方案时,我在这个平台上看到了很多你的贡献。:)
  • @justsomeone 谢谢 确实是一份很有帮助的文档。

标签: java datetime java-8 datetimeoffset


【解决方案1】:

例子:

public static void main(String[] args) {
    String input = "2021-02-11T09:00:01-07:00 --UTC";
    String timeStampCleanedUp = input.replaceFirst("\\s+--UTC", "");

    ZonedDateTime zonedDateTimeTmp = ZonedDateTime.parse(timeStampCleanedUp);

    //Conevert to other TimeZone, such as 'America/New_York':
    ZonedDateTime  zonedDateTime = zonedDateTimeTmp.withZoneSameInstant(ZoneId.of("America/New_York"));
    System.out.printf("Time stamp after zone time conversion: from '%s' to '%s'%n%n", zonedDateTimeTmp, zonedDateTime);

    //day, daynumber, date, month, year, time,timezone
    Map<String, String> result = new HashMap<>();

    result.put("DayOfWeek", zonedDateTime.getDayOfWeek().name()); //Day
    result.put("DayOfMonth", String.valueOf(zonedDateTime.getDayOfMonth())); //DayNumber
    result.put("Month", zonedDateTime.getMonth().name()); //Month
    result.put("Date", zonedDateTime.toLocalDate().toString()); //Date
    result.put("Month", zonedDateTime.getMonth().name()); //Month
    result.put("Year", String.valueOf(zonedDateTime.getYear())); //Year
    result.put("Time", zonedDateTime.toLocalTime().format(DateTimeFormatter.ISO_LOCAL_TIME)); //Time
    result.put("Offset", zonedDateTime.getOffset().getId()); //Offset
    result.put("TimeZone", zonedDateTime.getZone().getId()); //TimeZone

    result.keySet().stream().sorted().forEach(key -> System.out.printf("Key: '%s' => Value: '%s'%n", key, result.get(key)));
}

输出:

Time stamp after zone time conversion: from '2021-02-11T09:00:01-07:00' to '2021-02-11T11:00:01-05:00[America/New_York]'

Key: 'Date' => Value: '2021-02-11'
Key: 'DayOfMonth' => Value: '11'
Key: 'DayOfWeek' => Value: 'THURSDAY'
Key: 'Month' => Value: 'FEBRUARY'
Key: 'Offset' => Value: '-05:00'
Key: 'Time' => Value: '11:00:01'
Key: 'TimeZone' => Value: 'America/New_York'
Key: 'Year' => Value: '2021'

您可以使用 ZoneId.getAvailableZoneIds() 查看可用区域 ID:

ZoneId.getAvailableZoneIds().stream().forEach(System.out::println);

您可以在此处阅读有关 ZonId 和 ZoneOffset 的更多信息: https://docs.oracle.com/javase/10/docs/api/java/time/ZoneId.html https://docs.oracle.com/javase/10/docs/api/java/time/ZoneOffset.html

【讨论】:

  • 感谢您为解决我的问题所付出的努力。将很快对其进行测试,如有任何疑问会回复。:)
  • 很高兴您感谢您的努力。谢谢!
  • 嘿@DigitShifter 只是一个简单的问题,您已经添加了时区“America/New_York”,但这对于各种偏移量是否更通用。我不确定如何从给定的偏移量中获取时区。如果偏移量发生变化,此代码将失败。请多多指教。
  • 最终列表 timeZoneByUtc = ZoneId.getAvailableZoneIds().stream().map(ZoneId::of) .filter(z -> z.getRules().getOffset(Instant.now( )).equals(ZoneOffset.ofHours(-7))) .collect(Collectors.toList());
  • 我为-7尝试了上面的代码,但它是一个列表,可以选择哪个。
猜你喜欢
  • 1970-01-01
  • 2012-12-08
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多