java毫秒使用SimpleDateFormat转换时区问题解决

1.应用场景

当我们想将毫秒值转换成一段时间长度时,比如想将5400000毫秒转换成01::30:00这样的格式,结果使用SimpleDateFormat转换过后变成了09:30:00。(结尾有源码

String s = GalleryDateUtils.millisToString(5400000, "HH:mm:ss");
System.out.println(s);

在这里插入图片描述

2.原因解析

为什么只有小时变了?为什么不是其他数字而是9?
分析:如果将小于1小时的毫秒值转化,结果将是正确的,因为时间格式化将其转化为分钟和秒钟,不会受到时区的影响。
而我们是位于东八区,这个时间格式化类,将这个毫秒值看成了时间戳,自然会加上8,这样1变成9也就解释得通了。
为了确认,我们再将格式切换一下。

String s = GalleryDateUtils.millisToString(5400000, "yyyy-MM-dd HH:mm:ss");
System.out.println(s);

在这里插入图片描述
果然如我们所想,他是将毫秒值看成了时间戳,这样转换出来的自然是时间日期。

3.问题解决

给SimpleDateFormat设置时区为**“UTC”**。

class DateUtils {
    private static final ThreadLocal<Map<String, SimpleDateFormat>> SDF_THREAD_LOCAL =
            ThreadLocal.withInitial(() -> new HashMap<>());

    public static String millisToString(long millis, final String pattern) {
        return millisToString(millis, getSafeDateFormat(pattern));
    }

    private static SimpleDateFormat getSafeDateFormat(String pattern) {
        Map<String, SimpleDateFormat> sdfMap = SDF_THREAD_LOCAL.get();
        SimpleDateFormat simpleDateFormat = sdfMap.get(pattern);
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat(pattern);
            //添加一行设置时区的代码
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            sdfMap.put(pattern, simpleDateFormat);
        }
        return simpleDateFormat;
    }

    public static String millisToString(final long millis, final DateFormat format) {
        return format.format(new Date(millis));
    }
}

在这里插入图片描述
这样就如我们所愿,将毫秒转换为时间长度了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值