Java-将带有时区的以毫秒为单位的Unix时间转换为时间戳 [英] Java - convert unix time in miliseconds with time zone to timestamp
本文介绍了Java-将带有时区的以毫秒为单位的Unix时间转换为时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我有一个字符串:
I have a string for example:
1517439600000+0100
,我想以毫秒为单位将其转换为unix时间,没有时区。
我该怎么做?
and I want to convert it to unix time in miliseconds with no timezone. how can I do it?
p.s。
1)我不能使用 substring(0,5)
并向字符串添加3.6m毫秒,因为我的时区很多,一次是+0100,然后是+0200等...
p.s.
1) I cant use substring(0,5)
and add 3.6m miliseconds to the string because I have a lot of time zone once is +0100 and then is +0200 and etc...
2)如果更容易转换为常规时间戳,例如YYYY-mm-dd hh:mm:ss
,应该没问题。
2) if it more easier to convert to regular timestamp like YYYY-mm-dd hh:mm:ss it should be fine.
推荐答案
您可以执行以下操作:
String sign = "+";
String [] parts = time.split(sign);
Long millis = Long.parseLong(parts[0]);
String zoneOffset = sign + parts[1];
LocalDate date = Instant.ofEpochMilli(millis).atZone(ZoneOffset.of(zoneOffset)).toLocalDate();
这篇关于Java-将带有时区的以毫秒为单位的Unix时间转换为时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文