[英]Converting iso8601 date to unix timestamp in java
I have a date string 我有一个日期字符串
String s = "2014-09-01T19:22:43.000Z";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse(s);
But I get an exception: 但我得到一个例外:
Exception in thread "main" java.text.ParseException: Unparseable date: "2014-09-01T19:22:43.000Z"
How do I convert the above string to unix timestamp? 如何将上面的字符串转换为unix时间戳? Thanks
谢谢
How do I convert the above string to unix timestamp?
如何将上面的字符串转换为unix时间戳?
Instant.parse( "2014-09-01T19:22:43.000Z" )
.getEpochSecond()
The java.time.Instant
class can parse your input string with its standard ISO 8601 format. java.time.Instant
类可以使用标准ISO 8601格式解析输入字符串。 No need to specify a formatting pattern. 无需指定格式模式。
Instant instant = Instant.parse( "2014-09-01T19:22:43.000Z" );
To get a count of milliseconds since the epoch of 1970: 要获得自1970年代以来的毫秒数:
long millisecondsSinceUnixEpoch = instant.toEpochMilli() ;
For whole seconds since epoch of 1970-01-01T00:00:00Z: 自1970-01-01T00:00:00Z时代以来的整整秒
long secondsSinceUnixEpoch = instant.getEpochSecond() ;
Be aware of possible data loss when going to milliseconds or whole seconds. 在达到毫秒或整秒时,请注意可能的数据丢失 。 The java.time classes have nanosecond resolution, so any microseconds or nanoseconds present in the value will be truncated.
java.time类具有纳秒分辨率,因此值中存在的任何微秒或纳秒都将被截断。
Update The Joda-Time project is now in maintenance mode. 更新 Joda-Time项目现在处于维护模式。 The team advises migration to the java.time classes.
该团队建议迁移到java.time类。
The Joda-Time library makes this work easier. Joda-Time库使这项工作更容易。 Your ISO 8601 compliant string can be fed directly to a Joda-Time constructor.
您的ISO 8601兼容字符串可以直接提供给Joda-Time构造函数。 The built-in parser expects ISO 8601.
内置解析器需要ISO 8601。
DateTime dateTime = new DateTime( "2014-09-01T19:22:43.000Z" ) ;
What do you mean by a Unix timestamp? 你是什么意思的Unix时间戳? Some people mean a count of whole seconds since the first moment of 1970 UTC (the Unix epoch ) while ignoring leap seconds (see Unix Time ).
有些人的意思是自1970年UTC的第一个时刻(Unix 纪元 )以来的整秒数,而忽略了闰秒(参见Unix时间 )。 Some people mean a count of milliseconds or other resolution.
有些人的意思是毫秒或其他分辨率。
Note the use of a long
primitive rather than the more common int
. 注意使用
long
原语而不是更常见的int
。
For milliseconds, call getMillis()
. 对于毫秒,请调用
getMillis()
。
long millisecondsSinceUnixEpoch = dateTime.getMillis();
For whole seconds, divide by 1,000. 整整一秒,除以1,000。 Consider if you want rounding or truncation of the fractional seconds.
考虑是否要舍入或截断小数秒。
Normally I would suggest passing a DateTimeZone object along with your string to the DateTime constructor. 通常我会建议将DateTimeZone对象和您的字符串一起传递给DateTime构造函数。 But no need if all you want is a count since epoch.
但是没有必要,如果你想要的只是一个自纪元以来的计数。
The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
, Calendar
, & SimpleDateFormat
. 这些类取代了麻烦的旧遗留日期时间类,如
java.util.Date
, Calendar
和SimpleDateFormat
。
The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 。 And search Stack Overflow for many examples and explanations.
并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 .
规范是JSR 310 。
Where to obtain the java.time classes? 从哪里获取java.time类?
The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time.
该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as
Interval
, YearWeek
, YearQuarter
, and more . 您可以在这里找到一些有用的类,比如
Interval
, YearWeek
, YearQuarter
,和更多 。
X
is used for ISO 8601 time zone
in SimpleDateFormat
, not Z
X
用于SimpleDateFormat
ISO 8601 time zone
,而不是Z
Correct format is "yyyy-MM-dd'T'HH:mm:ss.SSSX"
正确的格式是
"yyyy-MM-dd'T'HH:mm:ss.SSSX"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.