Class TimezoneConverter
java.lang.Object
cloud.opencode.base.date.timezone.TimezoneConverter
Timezone converter for date/time conversions between timezones
时区转换器,用于不同时区之间的日期时间转换
This class provides fluent API for converting dates and times between different timezones with proper handling of DST and other timezone rules.
此类提供流畅的API,用于在不同时区之间转换日期和时间,并正确处理夏令时和其他时区规则。
Features | 主要功能:
- Fluent API for timezone conversion - 流畅的时区转换API
- Support for common timezone aliases - 支持常用时区别名
- DST-aware conversions - 夏令时感知的转换
- Batch conversion support - 批量转换支持
Usage Examples | 使用示例:
// Convert from Beijing to New York
ZonedDateTime nyTime = TimezoneConverter.from(ZoneId.of("Asia/Shanghai"))
.to(ZoneId.of("America/New_York"))
.convert(LocalDateTime.now());
// Using common timezone constants
ZonedDateTime result = TimezoneConverter.fromUTC()
.to(TimezoneConverter.CHINA)
.convert(Instant.now());
Security | 安全性:
- Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
- Null-safe: Yes (with explicit null checks) - 空值安全: 是(有明确的空值检查)
Performance | 性能特性:
- Time complexity: O(1) - all conversions are single arithmetic timezone offset operations - 时间复杂度: O(1) - 所有转换均为单次时区偏移算术运算
- Space complexity: O(1) - stores only source and target ZoneId references - 空间复杂度: O(1) - 仅存储源和目标 ZoneId 引用
- Since:
- JDK 25, opencode-base-date V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ZoneIdstatic final ZoneIdstatic final ZoneIdstatic final ZoneIdstatic final ZoneIdstatic final ZoneIdstatic final ZoneIdstatic final ZoneIdstatic final ZoneIdstatic final ZoneIdstatic final ZoneIdstatic final ZoneIdstatic final ZoneIdstatic final ZoneId -
Method Summary
Modifier and TypeMethodDescriptionconvert(long epochMilli) Converts epoch milliseconds to the target timezone 将毫秒时间戳转换到目标时区Converts an Instant to the target timezone 将Instant转换到目标时区convert(LocalDateTime dateTime) Converts a LocalDateTime to the target timezone 将LocalDateTime转换到目标时区static ZonedDateTimeconvert(LocalDateTime dateTime, ZoneId fromZone, ZoneId toZone) Converts a datetime between two timezones 在两个时区之间转换日期时间convert(ZonedDateTime zonedDateTime) Converts a ZonedDateTime to the target timezone 将ZonedDateTime转换到目标时区convertToLocal(LocalDateTime dateTime) Converts to LocalDateTime in the target timezone 转换为目标时区中的LocalDateTimeconvertToOffset(LocalDateTime dateTime) Converts to OffsetDateTime in the target timezone 转换为目标时区中的OffsetDateTimestatic Stringformat(Instant instant, ZoneId zone, DateTimeFormatter formatter) Formats a datetime in the specified timezone 以指定时区格式化日期时间static TimezoneConverterCreates a converter from the specified source timezone 从指定的源时区创建转换器static TimezoneConverterCreates a converter from China timezone 从中国时区创建转换器static TimezoneConverterCreates a converter from system default timezone 从系统默认时区创建转换器static TimezoneConverterfromUTC()Creates a converter from UTC 从UTC创建转换器static ZonedDateTimefromUTC(LocalDateTime utcDateTime, ZoneId toZone) Converts UTC to the specified timezone 将UTC转换为指定时区static doublegetOffsetHours(ZoneId zone1, ZoneId zone2) Gets the offset hours between two timezones 获取两个时区之间的偏移小时数static ZonedDateTimeGets the current time in the specified timezone 获取指定时区的当前时间Sets the target timezone 设置目标时区toChina()Sets the target timezone to China 设置目标时区为中国toSystem()Sets the target timezone to system default 设置目标时区为系统默认toUTC()Sets the target timezone to UTC 设置目标时区为UTCstatic ZonedDateTimetoUTC(LocalDateTime dateTime, ZoneId fromZone) Converts to UTC from the specified timezone 从指定时区转换为UTC
-
Field Details
-
UTC
-
CHINA
-
BEIJING
-
HONG_KONG
-
TOKYO
-
SEOUL
-
SINGAPORE
-
NEW_YORK
-
LOS_ANGELES
-
CHICAGO
-
LONDON
-
PARIS
-
BERLIN
-
SYDNEY
-
-
Method Details
-
from
Creates a converter from the specified source timezone 从指定的源时区创建转换器- Parameters:
sourceZone- the source timezone | 源时区- Returns:
- the converter | 转换器
-
fromUTC
Creates a converter from UTC 从UTC创建转换器- Returns:
- the converter | 转换器
-
fromChina
Creates a converter from China timezone 从中国时区创建转换器- Returns:
- the converter | 转换器
-
fromSystem
Creates a converter from system default timezone 从系统默认时区创建转换器- Returns:
- the converter | 转换器
-
to
Sets the target timezone 设置目标时区- Parameters:
targetZone- the target timezone | 目标时区- Returns:
- this converter | 此转换器
-
toUTC
Sets the target timezone to UTC 设置目标时区为UTC- Returns:
- this converter | 此转换器
-
toChina
Sets the target timezone to China 设置目标时区为中国- Returns:
- this converter | 此转换器
-
toSystem
Sets the target timezone to system default 设置目标时区为系统默认- Returns:
- this converter | 此转换器
-
convert
Converts a LocalDateTime to the target timezone 将LocalDateTime转换到目标时区- Parameters:
dateTime- the date time in source timezone | 源时区中的日期时间- Returns:
- the date time in target timezone | 目标时区中的日期时间
-
convert
Converts an Instant to the target timezone 将Instant转换到目标时区- Parameters:
instant- the instant | 时刻- Returns:
- the date time in target timezone | 目标时区中的日期时间
-
convert
Converts a ZonedDateTime to the target timezone 将ZonedDateTime转换到目标时区- Parameters:
zonedDateTime- the zoned date time | 带时区的日期时间- Returns:
- the date time in target timezone | 目标时区中的日期时间
-
convert
Converts epoch milliseconds to the target timezone 将毫秒时间戳转换到目标时区- Parameters:
epochMilli- the epoch milliseconds | 毫秒时间戳- Returns:
- the date time in target timezone | 目标时区中的日期时间
-
convertToLocal
Converts to LocalDateTime in the target timezone 转换为目标时区中的LocalDateTime- Parameters:
dateTime- the date time in source timezone | 源时区中的日期时间- Returns:
- the local date time in target timezone | 目标时区中的本地日期时间
-
convertToOffset
Converts to OffsetDateTime in the target timezone 转换为目标时区中的OffsetDateTime- Parameters:
dateTime- the date time in source timezone | 源时区中的日期时间- Returns:
- the offset date time in target timezone | 目标时区中的偏移日期时间
-
convert
Converts a datetime between two timezones 在两个时区之间转换日期时间- Parameters:
dateTime- the date time | 日期时间fromZone- the source timezone | 源时区toZone- the target timezone | 目标时区- Returns:
- the converted date time | 转换后的日期时间
-
fromUTC
Converts UTC to the specified timezone 将UTC转换为指定时区- Parameters:
utcDateTime- the UTC date time | UTC日期时间toZone- the target timezone | 目标时区- Returns:
- the converted date time | 转换后的日期时间
-
toUTC
Converts to UTC from the specified timezone 从指定时区转换为UTC- Parameters:
dateTime- the date time | 日期时间fromZone- the source timezone | 源时区- Returns:
- the UTC date time | UTC日期时间
-
now
Gets the current time in the specified timezone 获取指定时区的当前时间- Parameters:
zone- the timezone | 时区- Returns:
- the current time | 当前时间
-
getOffsetHours
-
format
Formats a datetime in the specified timezone 以指定时区格式化日期时间- Parameters:
instant- the instant | 时刻zone- the timezone | 时区formatter- the formatter | 格式化器- Returns:
- the formatted string | 格式化后的字符串
-