[英]How to convert this unix time string to java date
I got this time string "2015-07-16T03:58:24.932031Z", I need to convert to a java timestamp, I use the following code, seems the converted date is wrong? 我得到这个时间字符串“2015-07-16T03:58:24.932031Z”,我需要转换为java时间戳,我使用下面的代码,似乎转换日期是错误的?
public static void main(String[] args) throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
Date date = format.parse("2015-07-16T03:58:24.932031Z");
System.out.println("date: " + date);
System.out.println("timestamp: " + date.getTime());
}
output: 输出:
date: Thu Jul 16 04:13:56 CST 2015
timestamp: 1436991236031
Is my date format wrong? 我的日期格式错了吗?
Thanks in advance! 提前致谢!
You don't want to quote the Z, it's a timezone indicator. 你不想引用Z,这是一个时区指标。 Instead, use the
X
format specifier , for an ISO-8601 timezone. 而是使用
X
格式说明符 ,用于ISO-8601时区。
Separately, you may want to pre-process the string a bit, because the part at the end, .932031
, isn't milliseconds (remember, there are only 1000ms in a second). 另外,您可能希望稍微预处理字符串,因为末尾的部分
.932031
不是毫秒(记住,一秒钟内只有1000毫秒)。 Looking at that value, it's probably microseconds (millionths of a second). 看看那个值,它可能是微秒(百万分之一秒)。
SimpleDateFormat
doesn't have a format specifier for microseconds. SimpleDateFormat
没有微秒的格式说明符。 You could simply use a regular expression or other string manipulation to remove the last three digits of it to turn it into milliseconds instead. 您可以简单地使用正则表达式或其他字符串操作来删除它的最后三位数,将其转换为毫秒。
This (which assumes you've done that trimming) works: Live Copy 这(假设你已完成修剪)有效: 实时复制
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
// Note --------------------------------------------------------^^^^
Date date = format.parse("2015-07-16T03:58:24.932Z");
// Note trimming --------------------------------^
System.out.println("date: " + date);
System.out.println("timestamp: " + date.getTime());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.