Java将毫秒时间戳转换为相对于给定时区的日期 [英] Java convert millisecond timestamp to date with respect to given timezone

查看:64
本文介绍了Java将毫秒时间戳转换为相对于给定时区的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多资源,但是我无法获得的一件事就是在转换过程中转换毫秒时间戳,如何添加相应的时区。

I have seen many resources on this but one thing I cant get is when converting the millisecond timestamp, how I add a corresponding time zone, during the conversion process.

Date date = new Date(Long.valueOf(dateInMil*1000L);
SimpleDateFormat myDate = new SimpleDateFormat("EEE, MMM d, ''yy");
String formatted = myDate.format(date);

现在如果我有一个时区/偏移量字符串形式,即 -04:00或 +2:00如何将其应用于上述内容,以便获得正确的日期?

Now if I have a time zone/offset in string formate i.e. "-04:00" or "+2:00" how to apply it to the above so I can get the proper date ?

推荐答案

我在上一个项目中也做过类似的事情。您可以使用SimpleDateFormat类的setTimeZone方法。

I was doing a similar thing in my previous project.You can use setTimeZone method of SimpleDateFormat class. Something like this :

mydate.setTimeZone(TimeZone.getTimeZone("GMT -0400"));

这篇关于Java将毫秒时间戳转换为相对于给定时区的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆