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
    评论
### 回答1: 在 Java 中,可以使用 `java.time` 包中的 `Instant` 类将毫秒转换为时间。例如: ``` long millis = 1595487320000L; Instant instant = Instant.ofEpochMilli(millis); System.out.println(instant); // 输出:2020-07-23T09:52:00Z ``` 也可以使用 `java.util.Date` 类将毫秒转换为时间。例如: ``` long millis = 1595487320000L; Date date = new Date(millis); System.out.println(date); // 输出:Fri Jul 23 09:52:00 GMT+08:00 2021 ``` 注意: - `Instant` 类表示的是以 UTC 时区为基准的时间,并且精确到纳秒。 - `Date` 类表示的是以本地时区为基准的时间,并且精确到毫秒。 在 Java 8 以后,建议使用 `java.time` 包中的类来处理时间,因为它们提供了更多的功能和更好的性能。 ### 回答2: 在Java中,可以使用Date和DateFormat类来进行毫秒与时间的转换。 首先,将毫秒转换为时间,可以使用以下代码: ```java long milliseconds = 1591234567890L; // 假设要转换毫秒数 Date date = new Date(milliseconds); DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = format.format(date); ``` 这段代码将时间戳(milliseconds)转换为Date对象,并使用指定的日期格式将其格式化为字符串。你可以根据自己的需求修改日期格式。 接下来,将时间转换毫秒,可以使用以下代码: ```java String time = "2020-06-04 19:12:47"; // 假设要转换的时间字符串 DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = format.parse(time); long milliseconds = date.getTime(); ``` 这段代码将时间字符串(time)转换为Date对象,并使用相同的日期格式进行解析。然后,通过调用getTime()方法获取其对应的毫秒数。 需要注意的是,上面的代码中使用的是SimpleDateFormat类,它是DateFormat的一个具体实现类,用于处理日期格式化和解析。对于更复杂的日期格式,还可以使用java.time包中的DateTimeFormatter类来进行转换。 总结来说,Java中可以通过Date和DateFormat类来进行毫秒与时间的转换,具体的实现方式可以根据自己的需求选择不同的日期格式类。 ### 回答3: 在Java中,可以使用以下几种方法将毫秒转换为时间。 1. 使用Java时间类库: Java提供了`java.util.Date`类和`java.util.Calendar`类来处理日期和时间。可以使用以下代码将毫秒转换为时间: ```java long millis = 1587300000000L; Date date = new Date(millis); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = sdf.format(date); System.out.println(formattedDate); ``` 这将输出形如"2020-04-20 12:00:00"的时间字符串。 2. 使用`java.time`类库: 在Java 8及以上版本中,引入了新的日期和时间API(`java.time`包)。可以使用以下代码将毫秒转换为时间: ```java long millis = 1587300000000L; Instant instant = Instant.ofEpochMilli(millis); ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDate = formatter.format(zonedDateTime); System.out.println(formattedDate); ``` 这将输出与前面相同的时间字符串。 无论使用哪种方法,都需要将毫秒数作为参数传递给相应的类或方法,然后使用适当的格式化工具将其转换为所需的时间形式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值