Class TimezoneUtil
java.lang.Object
cloud.opencode.base.date.timezone.TimezoneUtil
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final ZoneIdCentral European Time (Berlin) 中欧时间(柏林)static final ZoneIdChina Standard Time (Beijing) 中国标准时间(北京)static final ZoneIdHong Kong Time 香港时间static final ZoneIdIndia Standard Time (Kolkata) 印度标准时间(加尔各答)static final ZoneIdJapan Standard Time (Tokyo) 日本标准时间(东京)static final ZoneIdKorea Standard Time (Seoul) 韩国标准时间(首尔)static final ZoneIdUK Time (London) 英国时间(伦敦)static final ZoneIdUS Pacific Time (Los Angeles) 美国太平洋时间(洛杉矶)static final ZoneIdUS Eastern Time (New York) 美国东部时间(纽约)static final ZoneIdCentral European Time (Paris) 中欧时间(巴黎)static final ZoneIdSingapore Time 新加坡时间static final ZoneIdSydney Time 悉尼时间static final ZoneIdUTC timezone UTC时区 -
Method Summary
Modifier and TypeMethodDescriptionstatic LocalDateTimeconvert(LocalDateTime dateTime, ZoneId fromZone, ZoneId toZone) Converts a LocalDateTime from one timezone to another 将LocalDateTime从一个时区转换为另一个时区static ZonedDateTimeconvert(ZonedDateTime dateTime, ZoneId zone) Converts a ZonedDateTime to another timezone 将ZonedDateTime转换为另一个时区findTimezones(String pattern) Gets timezones matching a pattern 获取匹配模式的时区static StringformatOffset(ZoneId zone) Formats the offset as a string (e.g., "+08:00") 将偏移格式化为字符串(如"+08:00")Gets all available timezone IDs 获取所有可用的时区IDGets all available timezones as ZoneId 获取所有可用的时区(ZoneId形式)static ZoneIdGets the system default timezone 获取系统默认时区static StringgetDisplayName(ZoneId zone, Locale locale) Gets the display name of a timezone 获取时区的显示名称static ZonedDateTimegetNextDstTransition(ZoneId zone) Gets the next DST transition for a timezone 获取时区的下一个夏令时转换static DurationGets the current offset for a timezone 获取时区的当前偏移static DurationgetOffsetBetween(ZoneId from, ZoneId to) Gets the offset between two timezones 获取两个时区之间的偏移static intgetOffsetHours(ZoneId zone) Gets the offset in hours for a timezone 获取时区的小时偏移static StringgetShortDisplayName(ZoneId zone, Locale locale) Gets the short display name of a timezone 获取时区的短显示名称static booleanisDaylightSavingTime(ZoneId zone) Checks if a timezone is currently in DST 检查时区当前是否处于夏令时static booleanisValidTimezone(String zoneId) Checks if a timezone ID is valid 检查时区ID是否有效static ZonedDateTimeGets the current time in the specified timezone 获取指定时区的当前时间static ZonedDateTimenowLocal()Gets the current time in the system default timezone 获取系统默认时区的当前时间static ZonedDateTimenowUtc()Gets the current time in UTC 获取UTC当前时间static InstanttoInstant(ZonedDateTime dateTime) Converts a ZonedDateTime to an Instant 将ZonedDateTime转换为Instantstatic LocalDateTimetoLocal(ZonedDateTime dateTime) Converts a ZonedDateTime to LocalDateTime in the same zone 将ZonedDateTime转换为同一时区的LocalDateTimestatic ZonedDateTimeConverts an Instant to ZonedDateTime in the specified timezone 将Instant转换为指定时区的ZonedDateTimestatic ZonedDateTimetoZoned(LocalDateTime dateTime, ZoneId zone) Converts a LocalDateTime to a ZonedDateTime in the specified timezone 将LocalDateTime转换为指定时区的ZonedDateTimestatic booleanusesDaylightSavingTime(ZoneId zone) Checks if a timezone uses DST 检查时区是否使用夏令时
-
Field Details
-
UTC
UTC timezone UTC时区 -
CHINA
China Standard Time (Beijing) 中国标准时间(北京) -
JAPAN
Japan Standard Time (Tokyo) 日本标准时间(东京) -
KOREA
Korea Standard Time (Seoul) 韩国标准时间(首尔) -
NEW_YORK
US Eastern Time (New York) 美国东部时间(纽约) -
LOS_ANGELES
US Pacific Time (Los Angeles) 美国太平洋时间(洛杉矶) -
LONDON
UK Time (London) 英国时间(伦敦) -
PARIS
Central European Time (Paris) 中欧时间(巴黎) -
BERLIN
Central European Time (Berlin) 中欧时间(柏林) -
INDIA
India Standard Time (Kolkata) 印度标准时间(加尔各答) -
SINGAPORE
Singapore Time 新加坡时间 -
HONG_KONG
Hong Kong Time 香港时间 -
SYDNEY
Sydney Time 悉尼时间
-
-
Method Details
-
nowUtc
Gets the current time in UTC 获取UTC当前时间- Returns:
- the current time in UTC | UTC当前时间
-
now
Gets the current time in the specified timezone 获取指定时区的当前时间- Parameters:
zone- the timezone | 时区- Returns:
- the current time | 当前时间
-
nowLocal
Gets the current time in the system default timezone 获取系统默认时区的当前时间- Returns:
- the current time | 当前时间
-
convert
Converts a ZonedDateTime to another timezone 将ZonedDateTime转换为另一个时区- Parameters:
dateTime- the date-time | 日期时间zone- the target timezone | 目标时区- Returns:
- the converted date-time | 转换后的日期时间
-
toZoned
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
Converts a ZonedDateTime to LocalDateTime in the same zone 将ZonedDateTime转换为同一时区的LocalDateTime- Parameters:
dateTime- the zoned date-time | 带时区的日期时间- Returns:
- the local date-time | 本地日期时间
-
toZoned
Converts an Instant to ZonedDateTime in the specified timezone 将Instant转换为指定时区的ZonedDateTime- Parameters:
instant- the instant | 瞬间zone- the timezone | 时区- Returns:
- the zoned date-time | 带时区的日期时间
-
toInstant
Converts a ZonedDateTime to an Instant 将ZonedDateTime转换为Instant- Parameters:
dateTime- the zoned date-time | 带时区的日期时间- Returns:
- the instant | 瞬间
-
convert
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
-
getOffsetBetween
-
getOffsetHours
Gets the offset in hours for a timezone 获取时区的小时偏移- Parameters:
zone- the timezone | 时区- Returns:
- the offset in hours | 小时偏移
-
formatOffset
-
getAllTimezoneIds
-
getAllTimezones
-
findTimezones
-
getDisplayName
-
getShortDisplayName
-
isValidTimezone
Checks if a timezone ID is valid 检查时区ID是否有效- Parameters:
zoneId- the timezone ID | 时区ID- Returns:
- true if valid | 如果有效返回true
-
getDefault
Gets the system default timezone 获取系统默认时区- Returns:
- the default timezone | 默认时区
-
isDaylightSavingTime
Checks if a timezone is currently in DST 检查时区当前是否处于夏令时- Parameters:
zone- the timezone | 时区- Returns:
- true if in DST | 如果处于夏令时返回true
-
usesDaylightSavingTime
Checks if a timezone uses DST 检查时区是否使用夏令时- Parameters:
zone- the timezone | 时区- Returns:
- true if uses DST | 如果使用夏令时返回true
-
getNextDstTransition
Gets the next DST transition for a timezone 获取时区的下一个夏令时转换- Parameters:
zone- the timezone | 时区- Returns:
- the next transition, or null if none | 下一个转换,如果没有则为null
-