Java8 time包时区处理

Java8中我们常用的时区类是ZoneDateTime,使用ZoneDateTime可以任意转Date的时区来显示,这些转换都是依照标准时间UTC0转时间,所以不同时区显示的时间不一样,可以通过偏移量来计算区分。
但是有一种情况是我们需要是我要转某个时间的时区而不是时间的转换或则显示转换。
如:真正的转时区通常发生在前后端传递时间参数,如果不是时间戳传递,传入后端的通常是默认系统JDK时区,所以如果前端有指定的时区需要后端转换后使用

时区转换代码:

  /**
     * 时区转换支持字符串时间
     * 缺省格式 "yyyy-MM-dd HH:mm:ss"
     * @param sourceDate
     * @param sourceZoneId
     * @param targetZoneId
     * @return
     * @throws ParseException
     */
    public static Date convertZoneTimeByString(String sourceDate, String sourceZoneId, String targetZoneId)  {
        try {
            if (sourceDate == null) {
                return null;
            }

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone(sourceZoneId));
            Date date = simpleDateFormat.parse(sourceDate);
            return Date.from(ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of(targetZoneId)).toInstant());

        } catch (ParseException e) {
            log.warn("DateBizUtils convertZoneTimeByString parse date error:{}", sourceDate);
            throw new RuntimeException("DateBizUtils convertZoneTimeByString parse date error", e);
        }
    }


    /**
     * 时区转换
     * 返回时间字符串
     * @param sourceDate
     * @param targetZoneId
     * @return
     * @throws ParseException
     */
    public static String convertZoneTimeToString(Date sourceDate, String targetZoneId)  {
        if (sourceDate == null) {
            return null;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone(targetZoneId));
        return simpleDateFormat.format(sourceDate);

    }

    /**
     * date类型时区转换
     * @param sourceZoneId
     * @param targetZoneId
     * @param sourceDate
     * @return
     */
    public static Date convertTargetTimeZoneDate(String sourceZoneId, String targetZoneId, Date sourceDate){
        if(sourceDate == null) {
            return null;
        }
        ZoneId sourceZone;
        if(sourceZoneId == null) {
            sourceZone = ZoneId.systemDefault();
        } else {
            sourceZone = ZoneId.of(sourceZoneId);
        }
        ZoneId targetZone;
        if(targetZoneId == null) {
            targetZone = ZoneId.systemDefault();
        } else {
            targetZone = ZoneId.of(targetZoneId);
        }
        return Date.from(LocalDateTime.ofInstant(sourceDate.toInstant(), targetZone).atZone(sourceZone).toInstant());
    }

    /**
     * patten date without time
     * @param date
     * @return
     */
    public static String convertPattenDate(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN_DATE);
        return simpleDateFormat.format(date) + " 00:00:00";
    }

注:时区转换如果使用Date类型作为入参和出参,会存在Date类型本身自带或则自动转换服务器时区的问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java对世界不同时区之间的时间转换提供了非常方便的处理方法。主要的处理类是`java.util.TimeZone`和`java.util.Calendar`。 首先,通过`TimeZone`类可以获取世界上所有的时区列表,可以通过`getAvailableIDs()`方法来获取,返回一个含所有时区ID的字符串数组。 然后,可以通过`TimeZone.getTimeZone(String)`方法来获取指定时区的`TimeZone`对象。这个方法接受一个时区ID作为参数,并返回对应的`TimeZone`对象。 接下来,使用`Calendar`类来进行时间的转换操作。`Calendar`类提供了许多方法,可以方便地进行日期和时间的计算、设置和获取。可以通过`Calendar.getInstance(TimeZone)`方法获取指定时区的`Calendar`实例。 对于时间转换,可以使用以下方法: - `setTimeZone(TimeZone)`:设置`Calendar`的时区。 - `get(int field)`:获取指定字段的值,例如年、月、日、小时、分钟等。 - `set(int field, int value)`:设置指定字段的值,例如年、月、日、小时、分钟等。 - `add(int field, int amount)`:增加或减少指定字段的值,例如增加一天、减少一小时等。 - `getTime()`:将`Calendar`对象转换为`Date`对象。 - `setTime(Date)`:将`Date`对象转换为`Calendar`对象。 通过这些方法,可以在不同的时区间进行时间的转换计算。可以根据不同的需求,在不同的场景下使用这些方法来处理世界不同时区之间的时间转换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值