简体   繁体   English

如何将此unix时间字符串转换为java日期

[英]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.

相关问题 如何使用 java.time 将带有可选时间的日期时间输入字符串转换为 UNIX 时间 - How to convert date time input string with optional time into UNIX time using java.time 如何在 Java 8 (Scala) 中将日期时间字符串转换为 long (UNIX Epoch Time) 毫秒 - How to convert a date time string to long (UNIX Epoch Time) Milliseconds in Java 8 (Scala) 如何在Java 8(Scala)中将日期时间字符串转换为long(UNIX纪元时间) - How to convert a date time string to long (UNIX Epoch Time) in Java 8 (Scala) 自Unix时代以来如何将日期/时间字符串转换为分钟? - How to convert date/time string into minutes since Unix epoch? 将日期时间字符串转换为Unix时间戳 - convert date time string to unix timestamp 将Unix时间转换为Java中的可读日期 - Convert Unix time into readable Date in Java 如何在Java中转换字符串和日期时间 - How to convert String and date time in java 如何在Java中将时间安全地转换为Unix时间戳? - How to safely convert time to unix timestamp in Java? 如何在不创建本地日期时间对象的情况下将字符串转换为 unix 纪元毫秒 - How to convert string to unix epoch milliseconds without creating local date time objects 如何在Java中将格式化日期(yyyy-MM-dd)转换为Unix时间? - How to convert Formatted Date (yyyy-MM-dd) to Unix time in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM