Java时区国际化解决方案

在这里插入图片描述
当用户所在时区和服务器所在时区不一致时,会产生时区相关问题,如时间显示错误、程序取得的时间和数据库存储的时间不一致、定时任务的触发没有跟随用户当前的时区等等问题.

统一拦截时区

/**
 *
 **/
@Component
@Slf4j
public class TimeZoneIdInterceptor implements HandlerInterceptor {
    
    private static final String TIME_ZONE_CODE = "timeZoneCode";
    
    /**
     * 在控制器方法处理之前执行的预处理方法
     * <p>
     * 该方法用于在控制器方法执行前进行一些预处理操作,例如检查用户是否登录、记录日志等 如果该方法返回false,则表示不执行控制器方法;如果返回true,则表示继续执行控制器方法
     *
     * @param request  请求对象,用于获取请求信息
     * @param response 响应对象,用于向客户端发送响应
     * @param handler  控制器方法的对象,用于执行具体的控制器方法
     * @return 是否执行控制器的处理逻辑,true表示执行,false表示不执行
     * @throws Exception 如果发生异常,将抛出异常
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //获取TimeZoneId 默认东八区
        String timeZoneId=request.getHeader(TIME_ZONE_CODE);
        if(StringUtilsPlus.isBlank(timeZoneId)){
            timeZoneId = ZoneIdEnum.CTT.getZoneIdName();
        }
        TimeZoneIdContext.setTimeZoneId(timeZoneId);
        return true;
    }
    
    
    /**
     * controller方法执行后
     * <p>
     * 该方法主要用于在控制器方法处理完请求后进行一些额外的处理或清理工作 它是拦截器中的一部分,用于在请求被控制器处理后立即执行自定义逻辑
     *
     * @param request      请求对象,用于访问请求信息
     * @param response     响应对象,用于控制响应给客户端的信息
     * @param handler      被执行的控制器方法的引用,用于识别和可能操作控制器方法
     * @param modelAndView ModelAndView对象,包含要渲染的视图和模型数据
     * @throws Exception 如果在处理过程中发生异常,可以将其抛出
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        log.info("postHandle执行{}", modelAndView);
        TimeZoneIdContext.removeTimeZoneId();
    }
    
    
}

GET请求及POST表单请求(RequestParam和PathVariable参数):
自定义spring mvc的参数解析器,配置Converter<String, T>转换器实现参数转换

@Configuration
@Slf4j
public class DateConverterConfig {
    /**
     * 创建一个字符串到LocalDateTime的转换器
     *
     * 该方法通过定义一个Converter接口的匿名类来实现字符串到LocalDateTime的对象转换
     * 主要用于Spring框架中,以便在数据绑定、类型转换时使用
     *
     * @return Converter<String, LocalDateTime> 返回一个实现了Converter接口的匿名类实例,
     *         用于将字符串转换为LocalDateTime对象
     */
    @Bean
    public Converter<String, LocalDateTime> localDateTimeConverter() {
        return new Converter<String, LocalDateTime>() {
            @Override
            public LocalDateTime convert(String source) {
                // String类型的日期字符串转为LocalDateTime类型,并加上时区处理
                // 如果源字符串不为空,则进行转换;否则返回null
                if(StringUtilsPlus.isNotBlank(source)){
                    return TimeZoneUtils.convertTimeZoneLocalDateTime(source, TimeZoneIdContext.getTimeZoneId().get(),
                            ZoneIdEnum.CTT.getZoneIdName());
                }
                return null;
            }
        };
    }
    
    /**
     * 创建一个字符串到日期的转换器
     *
     * 该方法通过定义一个Converter的Bean,实现了从字符串类型到Date类型的转换主要用于处理日期字符串,
     * 并考虑了时区的转换
     *
     * @return Converter<String, Date> 实现了从字符串到日期的转换功能
     */
    @Bean
    public Converter<String, Date> dateConverter() {
        return new Converter<String, Date>() {
            @Override
            public Date convert(String source) {
                // String类型的日期字符串转为LocalDateTime类型,并加上时区处理
                if(StringUtilsPlus.isNotBlank(source)){
                    return TimeZoneUtils.convertTimeZoneDate(source, TimeZoneIdContext.getTimeZoneId().get(),
                            ZoneIdEnum.CTT.getZoneIdName());
                }
                return null;
            }
        };
    }
}

  1. POST-application/json请求(RequestBody参数)在使用javabean作为入参时,javabean对象中的Date、LocalDateTime类型可以根据请求头中的时区字段自动转为应用服务当前时区的时间
  2. 接口返回对象时,对象中的Date、LocalDateTime类型的日期值,可以根据请求头中的时区字段,自动转为该时区的时间
@JsonComponent
public class DateJsonSerializerConfig {
    /**
     * 反序列化,其它时区的时间转为本地时间
     */
    public static class LocalDateTimeJsonDeserializer extends JsonDeserializer<LocalDateTime> {
        @Override
        public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            // 根据时区字段将日期转为本地时区时间
            // String类型日期转为LocalDateTime类型
            String timeZone = StringUtilsPlus.defaultIfEmpty(TimeZoneIdContext.getTimeZoneId().get(), ZoneIdEnum.CTT.getZoneIdName());
            String value  = jsonParser.getValueAsString();
            if(StringUtilsPlus.isNotBlank(value)){
                return TimeZoneUtils.convertTimeZoneLocalDateTime(value,timeZone,ZoneIdEnum.CTT.getZoneIdName());
            }
            return null;
        }
    }
    /**
     * 序列化,本地时间转为其它时区的时间
     */
    public static class LocalDateTimeJsonSerializer extends JsonSerializer<LocalDateTime> {
        
        @Override
        public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator,
                SerializerProvider serializerProvider) throws IOException {
            //  本地时间转对应时区的时间
            String timeZone = StringUtilsPlus.defaultIfEmpty(TimeZoneIdContext.getTimeZoneId().get(), ZoneIdEnum.CTT.getZoneIdName());
            if(localDateTime!=null) {
                jsonGenerator.writeString(TimeZoneUtils.convertTimeZoneLocalDateTime(localDateTime, timeZone).format(
                        DateTimeFormatter.ofPattern(GlobalConstants.YYYY_MM_DD_HH_MM_SS)));
            }
        }
    }
    
    
    /**
     * 反序列化,其它时区的时间转为本地时间
     */
    public static class DateTimeJsonDeserializer extends JsonDeserializer<Date> {
        @Override
        public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            // 根据时区字段将日期转为本地时区时间
            // String类型日期转为LocalDateTime类型
            String timeZone = StringUtilsPlus.defaultIfEmpty(TimeZoneIdContext.getTimeZoneId().get(), ZoneIdEnum.CTT.getZoneIdName());
            String value  = jsonParser.getValueAsString();
            if(StringUtilsPlus.isNotBlank(value)){
                return TimeZoneUtils.convertTimeZoneDate(value,timeZone,ZoneIdEnum.CTT.getZoneIdName());
            }
            return null;
        }
    }
    /**
     * 序列化,本地时间转为其它时区的时间
     */
    public static class DateTimeJsonSerializer extends JsonSerializer<Date> {
        
        @Override
        public void serialize(Date date, JsonGenerator jsonGenerator,
                SerializerProvider serializerProvider) throws IOException {
            //  本地时间转对应时区的时间
            String timeZone = StringUtilsPlus.defaultIfEmpty(TimeZoneIdContext.getTimeZoneId().get(), ZoneIdEnum.CTT.getZoneIdName());
            if(date!=null) {
                jsonGenerator.writeString(DateUtil.format(TimeZoneUtils.convertTimeZoneDate(date, timeZone),
                        GlobalConstants.YYYY_MM_DD_HH_MM_SS));
            }
        }
    }
}

时间时区相互转化工具类

/**
 * 时间时区转化
 **/
public class TimeZoneUtils {
    
    /***
     * 时间转化
     * 将给定的日期对象从一个时区转换到另一个时区
     * @param date 需要转换的日期对象
     * @param timeZone 目标时区
     * @return 转换后的日期对象,如果目标时区与默认时区相同,则返回原始日期对象
     */
    public static Date convertTimeZoneDate(Date date, String timeZone) {
        // 检查目标时区是否为默认时区,如果是,则无需转换,直接返回原始日期
        if (isDefaultTimeZone(timeZone)) {
            return date;
        }
        // 获取目标时区
        TimeZone otherZone = TimeZone.getTimeZone(timeZone);
        // 创建SimpleDateFormat对象,用于日期的格式化和解析
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 设置SimpleDateFormat的时区为其他时区
        sdf.setTimeZone(otherZone);
        // 用于存储转换后的日期对象
        Date otherZoneDate = null;
        try {
            // 将格式化后的日期字符串解析为日期对象,该对象现在处于目标时区
            // 使用纽约时区格式化日期
            String dateInNewYork = sdf.format(date);
            otherZoneDate = DateUtil.parse(dateInNewYork,"yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            // 如果发生异常,打印异常信息
            e.printStackTrace();
        }
        // 返回转换后的日期对象
        return otherZoneDate;
    }
    
    /**
     * 将LocalDate对象转换为特定时区的LocalDate对象 此方法主要用于在处理不同时区的日期时进行转换,以确保日期的正确解析和表示
     *
     * @param localDate 输入的LocalDate对象,表示日期
     * @param timeZone  目标时区的字符串表示,如"America/New_York"
     * @return 转换后特定时区的LocalDate对象
     */
    public static LocalDate convertTimeZoneLocalDate(LocalDate localDate, String timeZone) {
        if (isDefaultTimeZone(timeZone)) {
            return localDate;
        }
        // 指定时区
        ZoneId zoneId = ZoneId.of(timeZone);
        // 将LocalDate转换为指定时区的ZonedDateTime 该时区一天的开始时刻的
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId);
        // 返回转换后的LocalDate对象
        return zonedDateTime.toLocalDate();
    }
    /**
     * 将给定的时间字符串从一个时区转换到另一个时区
     * 此方法首先将时间字符串解析为本地日期时间对象,然后将其转换到源时区,
     * 最后将时间转换到目标时区,保持时间点不变
     *
     * @param dateTimeString 时间字符串,表示源时区中的本地日期和时间
     * @param sourceTimeZone   源时区的ID,例如"America/New_York"
     * @param targetTimeZone   目标时区的ID,例如"Europe/London"
     * @return 转换到目标时区后的本地日期时间对象
     */
    public static LocalDateTime convertTimeZoneLocalDateTime(String dateTimeString,String sourceTimeZone,String targetTimeZone) {
        // 获取源时区的ZoneId对象
        ZoneId sourceZoneId = ZoneId.of(sourceTimeZone);
        // 获取目标时区的ZoneId对象
        ZoneId targetZoneId = ZoneId.of(targetTimeZone);
        // 创建DateTimeFormatter指定日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(sourceZoneId);
        // 解析字符串到ZonedDateTime
        ZonedDateTime zonedDateTimeSource = ZonedDateTime.parse(dateTimeString, formatter);
        // 转换到另一个时区
        ZonedDateTime zonedDateTimeTarget = zonedDateTimeSource.withZoneSameInstant(targetZoneId);
        // 将源时区应用到本地日期时间,并转换到目标时区,最后返回转换后的本地日期时间
        return zonedDateTimeTarget.toLocalDateTime();
    }
    
    /**
     * 转换日期时间字符串从一个时区到另一个时区
     * 此方法首先根据源时区解析输入的日期时间字符串,然后将其转换到目标时区
     * 最后返回转换后的日期时间的Date对象
     *
     * @param dateTimeString 日期时间字符串,格式为"yyyy-MM-dd HH:mm:ss"
     * @param sourceTimeZone 源时区ID,如"Asia/Shanghai"
     * @param targetTimeZone 目标时区ID,如"America/New_York"
     * @return 转换时区后的日期时间对象
     */
    public static Date convertTimeZoneDate(String dateTimeString,String sourceTimeZone,String targetTimeZone) {
        // 获取源时区的ZoneId对象
        ZoneId sourceZoneId = ZoneId.of(sourceTimeZone);
        // 获取目标时区的ZoneId对象
        ZoneId targetZoneId = ZoneId.of(targetTimeZone);
        // 创建DateTimeFormatter指定日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(sourceZoneId);
        // 解析字符串到ZonedDateTime
        ZonedDateTime zonedDateTimeSource = ZonedDateTime.parse(dateTimeString, formatter);
        // 转换到另一个时区
        ZonedDateTime zonedDateTimeTarget = zonedDateTimeSource.withZoneSameInstant(targetZoneId);
        // 将源时区应用到本地日期时间,并转换到目标时区,最后返回转换后的本地日期时间
        LocalDateTime targetLocalDateTime = zonedDateTimeTarget.toLocalDateTime();
        Date date = Date.from(targetLocalDateTime.atZone(targetZoneId).toInstant());
        return date;
    }
    /**
     * 将LocalDateTime对象从默认时区转换到指定时区
     *
     * @param localDateTime 需要转换的LocalDateTime对象
     * @param timeZone      目标时区的ID字符串
     * @return 转换后的LocalDateTime对象
     */
    public static LocalDateTime convertTimeZoneLocalDateTime(LocalDateTime localDateTime, String timeZone) {
        if (isDefaultTimeZone(timeZone)) {
            return localDateTime;
        }
        // 默认时区
        ZoneId sourceZoneId = currentZoneId();
        ZonedDateTime sourceZonedDateTime = localDateTime.atZone(sourceZoneId);
        // 新时区
        ZoneId targetZoneId = ZoneId.of(timeZone);
        ZonedDateTime targetZonedDateTime = sourceZonedDateTime.withZoneSameInstant(targetZoneId);
        // 时区转换
        return targetZonedDateTime.toLocalDateTime();
    }
    
    /**
     * 是否是默认时区
     *
     * @param timeZone
     * @return
     */
    public static boolean isDefaultTimeZone(String timeZone) {
        return StringUtilsPlus.isBlank(timeZone) || currentZoneId().getId().toUpperCase()
                .equals(timeZone.toUpperCase());
    }
    
    /**
     * 指定默认时区
     *
     * @return java.time.ZoneId 返回指定的时区ID
     */
    public static ZoneId specifyDefaultZone() {
        // 创建一个时区ID对象,指定时区为中国的北京时间
        ZoneId zoneId = ZoneId.of(ZoneIdEnum.CTT.getZoneIdName());
        // 返回创建的时区ID对象
        return zoneId;
    }
    
    /**
     * 获取当前系统的时区ID
     * <p>
     * 此方法用于识别和返回系统默认的时区ID它在需要时区信息来处理日期和时间的场景中特别有用
     *
     * @return ZoneId 当前系统的时区ID
     */
    public static ZoneId currentZoneId() {
        // 获取系统默认时区的时区ID
        ZoneId systemDefaultZone = ZoneId.systemDefault();
        return systemDefaultZone;
    }
    
    /**
     * 指定时区获取当前日期
     * 此方法主要解决在不同地区运行代码时获取的日期不一致的问题
     * 通过指定默认时区,确保在任何地方运行都能获取到正确的日期
     *
     * @return 返回指定时区的当前日期
     */
    public static LocalDate specifyTimeZoneLocalDate() {
        return LocalDate.now(specifyDefaultZone());
    }
    /**
     * 获取指定时区的当前日期和时间
     * 此方法通过指定默认时区来获取当前的日期和时间,主要用于需要考虑时区的因素以确保时间一致性的场景
     *
     * @return 返回指定时区的当前日期和时间
     */
    public static LocalDateTime specifyTimeZoneLocalTimeDate() {
        return LocalDateTime.now(specifyDefaultZone());
    }
    
    
    /**
     * 获取  亚洲/上海 当前时间
     *
     * 此方法旨在提供一个简单的方式来获取当前时间,并将其转换为CTT时区的时间
     * 它使用了更通用的convertTimeZoneLocalDateTime方法来执行实际的时区转换
     *
     * @return 当前时间转换为CTT时区后的LocalDateTime
     */
    public static LocalDateTime cttLocalDateTime() {
        return convertTimeZoneLocalDateTime(LocalDateTime.now(), ZoneIdEnum.CTT.getZoneIdName());
    }
}

public class TimeZoneIdContext {
    
    private static final ThreadLocal<String> TIME_ZONE_ID = new ThreadLocal<>();
    
    public static void setTimeZoneId(String timeZoneId) {
        TIME_ZONE_ID.set(timeZoneId);
    }
    
    public static ThreadLocal<String> getTimeZoneId() {
        return TIME_ZONE_ID;
    }
    
    public static void removeTimeZoneId() {
        TIME_ZONE_ID.remove();
    }
    
    /**
     * 获取时区id 如果为空则返回默认时区id
     *
     * @return
     */
    public static String get() {
        String zoneId = TIME_ZONE_ID.get();
        if (StringUtils.isEmpty(zoneId)) {
            return ZoneIdEnum.CTT.getZoneIdName();
        }
        return zoneId;
    }
}

public enum ZoneIdEnum {
    
    /**
     * "Australia/Darwin","澳洲/达尔文"
     */
    ACT("Australia/Darwin", "澳洲/达尔文"),
    
    /**
     * "Australia/Sydney","澳洲/悉尼"
     */
    AET("Australia/Sydney", "澳洲/悉尼"),
    
    /**
     * "America/Argentina/Buenos_Aires","美洲/阿根廷/布宜诺斯艾利斯"
     */
    AGT("America/Argentina/Buenos_Aires", "美洲/阿根廷/布宜诺斯艾利斯"),
    
    /**
     * "Africa/Cairo","非洲/开罗"
     */
    ART("Africa/Cairo", "非洲/开罗"),
    
    /**
     * "America/Anchorage","美洲/安克雷奇"
     */
    AST("America/Anchorage", "美洲/安克雷奇"),
    
    /**
     * "America/Sao_Paulo","美洲/圣保罗"
     */
    BET("America/Sao_Paulo", "美洲/圣保罗"),
    /**
     * "Asia/Dhaka","亚洲/达卡"
     */
    BST("Asia/Dhaka", "亚洲/达卡"),
    
    /**
     * "Africa/Harare","非洲/哈拉雷"
     */
    CAT("Africa/Harare", "非洲/哈拉雷"),
    
    /**
     * "America/St_Johns","美洲/圣约翰"
     */
    CNT("America/St_Johns", "美洲/圣约翰"),
    
    /**
     * "America/Chicago","美洲/芝加哥"
     */
    CST("America/Chicago", "美洲/芝加哥"),
    
    /**
     * "Asia/Shanghai","亚洲/上海"
     */
    CTT("Asia/Shanghai", "亚洲/上海"),
    
    /**
     * "Africa/Addis_Ababa","非洲/亚的斯亚贝巴"
     */
    EAT("Africa/Addis_Ababa", "非洲/亚的斯亚贝巴"),
    
    /**
     * "Europe/Paris","欧洲/巴黎"
     */
    ECT("Europe/Paris", "欧洲/巴黎"),
    
    /**
     * "America/Indiana/Indianapolis","美洲/印第安纳州/印第安纳波利斯"
     */
    IET("America/Indiana/Indianapolis", "美洲/印第安纳州/印第安纳波利斯"),
    
    /**
     * "Asia/Kolkata","亚洲/加尔各答"
     */
    IST("Asia/Kolkata", "亚洲/加尔各答"),
    
    /**
     * "Asia/Tokyo","亚洲/东京"
     */
    JST("Asia/Tokyo", "亚洲/东京"),
    
    /**
     * "Pacific/Apia","太平洋/阿皮亚"
     */
    MIT("Pacific/Apia", "太平洋/阿皮亚"),
    
    /**
     * "Asia/Yerevan","亚洲/埃里温"
     */
    NET("Asia/Yerevan", "亚洲/埃里温"),
    
    /**
     * "Pacific/Auckland","太平洋/奥克兰"
     */
    NST("Pacific/Auckland", "太平洋/奥克兰"),
    
    /**
     * "Asia/Karachi","亚洲/卡拉奇"
     */
    PLT("Asia/Karachi", "亚洲/卡拉奇"),
    
    /**
     * "America/Phoenix","美洲/凤凰城"
     */
    PNT("America/Phoenix", "美洲/凤凰城"),
    
    /**
     * "America/Puerto_Rico","美洲/波多黎各"
     */
    PRT("America/Puerto_Rico", "美洲/波多黎各"),
    
    /**
     * "America/Los_Angeles","美洲/洛杉矶"
     */
    PST("America/Los_Angeles", "美洲/洛杉矶"),
    
    /**
     * "Pacific/Guadalcanal","太平洋/瓜达尔卡纳尔岛"
     */
    SST("Pacific/Guadalcanal", "太平洋/瓜达尔卡纳尔岛"),
    
    /**
     * "Asia/Ho_Chi_Minh","亚洲/胡志明市"
     */
    VST("Asia/Ho_Chi_Minh", "亚洲/胡志明市"),
    
    /**
     * "-05:00","东部标准时间"(纽约、华盛顿)
     */
    EST("-05:00", "东部标准时间"),
    
    /**
     * "-07:00","山地标准时间"
     */
    MST("-07:00", "山地标准时间"),
    
    /**
     * "-10:00","夏威夷-阿留申标准时区"
     */
    HST("-10:00", "夏威夷-阿留申标准时区"),;
    
    private final String zoneIdName;
    private final String zoneIdNameCn;
    
    public String getZoneIdName() {
        return zoneIdName;
    }
    
    public String getZoneIdNameCn() {
        return zoneIdNameCn;
    }
    
    ZoneIdEnum(String zoneIdName, String zoneIdNameCn) {
        this.zoneIdName = zoneIdName;
        this.zoneIdNameCn = zoneIdNameCn;
    }
}

参考:链接

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值