[英]How to get GMT date from Unix epoch time milliseconds?
I trying convert unix milliseconds to gmt date, I need only hours and minutes, but results are incorrect according to online converters. 我尝试将unix毫秒转换为gmt日期,我只需要几小时和几分钟,但根据在线转换器,结果不正确。
What I need 我需要的
Here is my code: 这是我的代码:
public static void main(String[] args) {
long time = 1438050023;
// TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time / 1000);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss dd MM yyyy");
simpleDateFormat.setTimeZone(calendar.getTimeZone());
System.out.println(simpleDateFormat.format(calendar.getTime()));
}
Result: 结果:
03:23:58 01 01 1970
Change calendar.setTimeInMillis(time / 1000)
to calendar.setTimeInMillis(time * 1000)
将
calendar.setTimeInMillis(time / 1000)
更改为calendar.setTimeInMillis(time * 1000)
The number of milliseconds is 1000 times the number of seconds; 毫秒数是秒数的1000倍; not 1/1000 the number.
数字不是1/1000。
public static String ConvertMillistoDatetime(long millis) {
long second = (millis / 1000) % 60;
long minute = (millis / (1000 * 60)) % 60;
long hour = (millis / (1000 * 60 * 60)) % 24;
return String.format("%02d:%02d:%02d", hour, minute, second);
}
Try this you can keep seconds optional here 试试这个你可以在这里保持秒可选
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.