时区转换工具

  1. 工具类编写

    public class ZoneTimeUtils {
    
        private ZoneTimeUtils(){}
    
        /**
         * 加时区则返回时间为负数,负时区则为正数
         * @param timezone 时区
         * @param targetTimezone 目标时区
         * @return 与东八区时间差(比东八区时间更快则返回负数例如:+09:00区,比东八区更慢的时区返回正数例如:+7:00区)
         */
        public static int parseZoneGmtOffset(String timezone, String targetTimezone){
            int gmtValue = parseGmtValue(timezone);
            int targetGmtValue = parseGmtValue(targetTimezone);
            return  targetGmtValue - gmtValue;
        }
    
        public static int parseGmtValue(String timezone){
            if ("GMT".equals(timezone)){
                return 0 ;
            }
            String tmpValue = timezone.substring(3, 6);
            return Integer.parseInt(tmpValue);
        }
    
    
        /**
         * 根据某一个时区的时间 获取另一个时区的时间
         * @param sourceDate 指定时区的时间
         * @param timeZoneId 指定时区
         * @param targetTimeZoneId 目标时区
         * @return 目标时区的时间
         */
        public static Date parseZoneDateTime(
                Date sourceDate, String timeZoneId, String targetTimeZoneId){
            TimeZone sourceTimeZone = TimeZone.getTimeZone(timeZoneId);
            TimeZone targetTimeZone = TimeZone.getTimeZone(targetTimeZoneId);
            long targetTime = sourceDate.getTime() - sourceTimeZone.getRawOffset() + targetTimeZone.getRawOffset();
            return new Date(targetTime);
        }
    
        /**
         * 将时间转换为指定时区的时间
         * @param dateTime 时间字符串
         * @param timeZone 时区
         * @param targetTimeZone 转换到指定时区
         * @return 指定时区的时间
         * @throws ParseException 日期格式异常
         */
        public static String parseZoneDateTime(
                String dateTime, String timeZone, String targetTimeZone, String format) {
            try{
                // 输入的日期时间字符串和时区
                // 创建SimpleDateFormat对象,并设置时区为指定的时区
                SimpleDateFormat inputFormat = new SimpleDateFormat(format);
                inputFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
                // 将输入的日期时间字符串转换为Date对象
                Date inputDate = inputFormat.parse(dateTime);
                // 创建SimpleDateFormat对象,并将时区设置为东八区
                SimpleDateFormat outputFormat = new SimpleDateFormat(format);
                outputFormat.setTimeZone(TimeZone.getTimeZone(targetTimeZone));
                // 将Date对象转换为东八区的日期时间字符串
                return outputFormat.format(inputDate);
            }catch (ParseException e){
                throw BusinessException.newInstance(ResultMsgCode.INTERNAL_SERVER_ERROR, "parse datetime ["+dateTime+"] error !");
            }
        }
    
        public static String parseZoneTime(
                String time, String timeZone, String targetTimeZone, String format) {
            String dateTime = "2099-01-10 " + time;
            return parseZoneDateTime(dateTime, timeZone, targetTimeZone, format);
        }
    
        /**
         * 在指定时区,指定天是否在当前日期之后
         * @param specialDate 指定日期
         * @param timeZone 指定时区
         * @return 日期是否在当前日期之后
         */
        public static boolean isDateAfterNow(LocalDate specialDate, String timeZone){
            // 获取指定时区的时间
            ZoneId zoneId = ZoneId.of(timeZone.trim());
            //1. 获取指定时区的当前时间
            ZonedDateTime zonedNowTime = ZonedDateTime.now(zoneId);
            //2. 获取指定时区的指定时间
            int year = specialDate.getYear();
            int monthValue = specialDate.getMonthValue();
            int dayOfMonth = specialDate.getDayOfMonth();
            int hour = 0;
            int minus = 0 ;
            int second = 0 ;
            int nanoOfSecond = 0 ;
            ZonedDateTime zonedSpecialTime = ZonedDateTime.of(year, monthValue, dayOfMonth, hour, minus, second, nanoOfSecond, zoneId);
            return zonedSpecialTime.isAfter(zonedNowTime) ;
        }
    
        public static LocalDateTime getLocalDateTime(String timeZone){
            // 获取指定时区的时间
            ZoneId zoneId = ZoneId.of(timeZone.trim());
            //1. 获取指定时区的当前时间
            ZonedDateTime zonedNowTime = ZonedDateTime.now(zoneId);
            return zonedNowTime.toLocalDateTime() ;
        }
    
    
        public static LocalDateTime convertToLocal(
                LocalDateTime dateTime, String localTimeZoneId, String targetTimeZoneId) {
            ZonedDateTime localZonedDateTime = dateTime.atZone(ZoneId.of(localTimeZoneId));
            //
            ZoneId targetZoneId = ZoneId.of(targetTimeZoneId);
            ZonedDateTime targetZonedDateTime = localZonedDateTime.withZoneSameInstant(targetZoneId);
            return targetZonedDateTime.toLocalDateTime();
        }
    
        public static LocalDateTime convertToLocal(
                LocalDateTime dateTime, String targetTimeZoneId) {
            ZoneId zoneId = ZoneId.systemDefault();
            String zoneIdValue = zoneId.getId();
            return convertToLocal(dateTime, zoneIdValue, targetTimeZoneId);
        }
    
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值