Class TimezoneUtil

java.lang.Object
cloud.opencode.base.date.timezone.TimezoneUtil

public final class TimezoneUtil extends Object
Utility class for timezone operations 时区操作工具类

This class provides various utilities for working with timezones, including conversion, offset calculations, and timezone information.

此类提供各种时区操作工具,包括转换、偏移计算和时区信息。

Features | 主要功能:

  • Get all available timezones - 获取所有可用时区
  • Timezone offset calculations - 时区偏移计算
  • Convert between timezones - 时区之间转换
  • Common timezone constants - 常用时区常量

Usage Examples | 使用示例:

// Get current time in different timezones
ZonedDateTime utc = TimezoneUtil.nowUtc();
ZonedDateTime beijing = TimezoneUtil.now(TimezoneUtil.CHINA);

// Convert between timezones
ZonedDateTime newYork = TimezoneUtil.convert(beijing, TimezoneUtil.NEW_YORK);

// Get timezone offset
Duration offset = TimezoneUtil.getOffset(TimezoneUtil.CHINA);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes (with explicit null checks) - 空值安全: 是(有明确的空值检查)

Performance | 性能特性:

  • Time complexity: O(n) for getAllTimezones and findTimezones where n=number of available timezone IDs; O(1) for individual conversions and offset lookups - 时间复杂度: getAllTimezones 和 findTimezones 为 O(n),n 为可用时区 ID 数量;单次转换和偏移查询为 O(1)
  • Space complexity: O(n) for getAllTimezones result list; O(1) for conversion operations - 空间复杂度: getAllTimezones 结果列表为 O(n);转换操作为 O(1)
Since:
JDK 25, opencode-base-date V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

    • UTC

      public static final ZoneId UTC
      UTC timezone UTC时区
    • CHINA

      public static final ZoneId CHINA
      China Standard Time (Beijing) 中国标准时间(北京)
    • JAPAN

      public static final ZoneId JAPAN
      Japan Standard Time (Tokyo) 日本标准时间(东京)
    • KOREA

      public static final ZoneId KOREA
      Korea Standard Time (Seoul) 韩国标准时间(首尔)
    • NEW_YORK

      public static final ZoneId NEW_YORK
      US Eastern Time (New York) 美国东部时间(纽约)
    • LOS_ANGELES

      public static final ZoneId LOS_ANGELES
      US Pacific Time (Los Angeles) 美国太平洋时间(洛杉矶)
    • LONDON

      public static final ZoneId LONDON
      UK Time (London) 英国时间(伦敦)
    • PARIS

      public static final ZoneId PARIS
      Central European Time (Paris) 中欧时间(巴黎)
    • BERLIN

      public static final ZoneId BERLIN
      Central European Time (Berlin) 中欧时间(柏林)
    • INDIA

      public static final ZoneId INDIA
      India Standard Time (Kolkata) 印度标准时间(加尔各答)
    • SINGAPORE

      public static final ZoneId SINGAPORE
      Singapore Time 新加坡时间
    • HONG_KONG

      public static final ZoneId HONG_KONG
      Hong Kong Time 香港时间
    • SYDNEY

      public static final ZoneId SYDNEY
      Sydney Time 悉尼时间
  • Method Details

    • nowUtc

      public static ZonedDateTime nowUtc()
      Gets the current time in UTC 获取UTC当前时间
      Returns:
      the current time in UTC | UTC当前时间
    • now

      public static ZonedDateTime now(ZoneId zone)
      Gets the current time in the specified timezone 获取指定时区的当前时间
      Parameters:
      zone - the timezone | 时区
      Returns:
      the current time | 当前时间
    • nowLocal

      public static ZonedDateTime nowLocal()
      Gets the current time in the system default timezone 获取系统默认时区的当前时间
      Returns:
      the current time | 当前时间
    • convert

      public static ZonedDateTime convert(ZonedDateTime dateTime, ZoneId zone)
      Converts a ZonedDateTime to another timezone 将ZonedDateTime转换为另一个时区
      Parameters:
      dateTime - the date-time | 日期时间
      zone - the target timezone | 目标时区
      Returns:
      the converted date-time | 转换后的日期时间
    • toZoned

      public static ZonedDateTime toZoned(LocalDateTime dateTime, ZoneId zone)
      Converts a LocalDateTime to a ZonedDateTime in the specified timezone 将LocalDateTime转换为指定时区的ZonedDateTime
      Parameters:
      dateTime - the local date-time | 本地日期时间
      zone - the timezone | 时区
      Returns:
      the zoned date-time | 带时区的日期时间
    • toLocal

      public static LocalDateTime toLocal(ZonedDateTime dateTime)
      Converts a ZonedDateTime to LocalDateTime in the same zone 将ZonedDateTime转换为同一时区的LocalDateTime
      Parameters:
      dateTime - the zoned date-time | 带时区的日期时间
      Returns:
      the local date-time | 本地日期时间
    • toZoned

      public static ZonedDateTime toZoned(Instant instant, ZoneId zone)
      Converts an Instant to ZonedDateTime in the specified timezone 将Instant转换为指定时区的ZonedDateTime
      Parameters:
      instant - the instant | 瞬间
      zone - the timezone | 时区
      Returns:
      the zoned date-time | 带时区的日期时间
    • toInstant

      public static Instant toInstant(ZonedDateTime dateTime)
      Converts a ZonedDateTime to an Instant 将ZonedDateTime转换为Instant
      Parameters:
      dateTime - the zoned date-time | 带时区的日期时间
      Returns:
      the instant | 瞬间
    • convert

      public static LocalDateTime convert(LocalDateTime dateTime, ZoneId fromZone, ZoneId toZone)
      Converts a LocalDateTime from one timezone to another 将LocalDateTime从一个时区转换为另一个时区
      Parameters:
      dateTime - the local date-time | 本地日期时间
      fromZone - the source timezone | 源时区
      toZone - the target timezone | 目标时区
      Returns:
      the converted local date-time | 转换后的本地日期时间
    • getOffset

      public static Duration getOffset(ZoneId zone)
      Gets the current offset for a timezone 获取时区的当前偏移
      Parameters:
      zone - the timezone | 时区
      Returns:
      the offset as Duration | 偏移(Duration形式)
    • getOffsetBetween

      public static Duration getOffsetBetween(ZoneId from, ZoneId to)
      Gets the offset between two timezones 获取两个时区之间的偏移
      Parameters:
      from - the source timezone | 源时区
      to - the target timezone | 目标时区
      Returns:
      the offset difference | 偏移差
    • getOffsetHours

      public static int getOffsetHours(ZoneId zone)
      Gets the offset in hours for a timezone 获取时区的小时偏移
      Parameters:
      zone - the timezone | 时区
      Returns:
      the offset in hours | 小时偏移
    • formatOffset

      public static String formatOffset(ZoneId zone)
      Formats the offset as a string (e.g., "+08:00") 将偏移格式化为字符串(如"+08:00")
      Parameters:
      zone - the timezone | 时区
      Returns:
      the formatted offset | 格式化的偏移
    • getAllTimezoneIds

      public static Set<String> getAllTimezoneIds()
      Gets all available timezone IDs 获取所有可用的时区ID
      Returns:
      the set of timezone IDs | 时区ID集合
    • getAllTimezones

      public static List<ZoneId> getAllTimezones()
      Gets all available timezones as ZoneId 获取所有可用的时区(ZoneId形式)
      Returns:
      the list of ZoneId | ZoneId列表
    • findTimezones

      public static List<String> findTimezones(String pattern)
      Gets timezones matching a pattern 获取匹配模式的时区
      Parameters:
      pattern - the pattern to match (case-insensitive) | 要匹配的模式(不区分大小写)
      Returns:
      the list of matching timezone IDs | 匹配的时区ID列表
    • getDisplayName

      public static String getDisplayName(ZoneId zone, Locale locale)
      Gets the display name of a timezone 获取时区的显示名称
      Parameters:
      zone - the timezone | 时区
      locale - the locale | 区域设置
      Returns:
      the display name | 显示名称
    • getShortDisplayName

      public static String getShortDisplayName(ZoneId zone, Locale locale)
      Gets the short display name of a timezone 获取时区的短显示名称
      Parameters:
      zone - the timezone | 时区
      locale - the locale | 区域设置
      Returns:
      the short display name | 短显示名称
    • isValidTimezone

      public static boolean isValidTimezone(String zoneId)
      Checks if a timezone ID is valid 检查时区ID是否有效
      Parameters:
      zoneId - the timezone ID | 时区ID
      Returns:
      true if valid | 如果有效返回true
    • getDefault

      public static ZoneId getDefault()
      Gets the system default timezone 获取系统默认时区
      Returns:
      the default timezone | 默认时区
    • isDaylightSavingTime

      public static boolean isDaylightSavingTime(ZoneId zone)
      Checks if a timezone is currently in DST 检查时区当前是否处于夏令时
      Parameters:
      zone - the timezone | 时区
      Returns:
      true if in DST | 如果处于夏令时返回true
    • usesDaylightSavingTime

      public static boolean usesDaylightSavingTime(ZoneId zone)
      Checks if a timezone uses DST 检查时区是否使用夏令时
      Parameters:
      zone - the timezone | 时区
      Returns:
      true if uses DST | 如果使用夏令时返回true
    • getNextDstTransition

      public static ZonedDateTime getNextDstTransition(ZoneId zone)
      Gets the next DST transition for a timezone 获取时区的下一个夏令时转换
      Parameters:
      zone - the timezone | 时区
      Returns:
      the next transition, or null if none | 下一个转换,如果没有则为null