[英]convert string timestamp to ISO 8601 compliant string
I have a timestamp and offset in string format as shown below in two different variables: 我有一个时间戳和字符串格式的偏移量,如下所示,在两个不同的变量中:
01/14/2016 07:37:36PM
-08:00
I want to convert above timestamp into ISO 8601 compliant String, with milliseconds and timezone
so it should look like this after conversion: 我想将上述时间戳转换为
ISO 8601 compliant String, with milliseconds and timezone
因此转换后应如下所示:
2016-01-14T19:37:36-08:00
How can I do that? 我怎样才能做到这一点? I am using jodatime library.
我正在使用jodatime库。
The newer java.time
classes work so well with ISO 8601 strings. 较新的
java.time
类可与ISO 8601字符串配合使用。
String dateTimeString = "01/14/2016 07:37:36PM";
String offsetString = "-08:00";
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString,
DateTimeFormatter.ofPattern("MM/dd/uuuu hh:mm:ssa"));
ZoneOffset offset = ZoneOffset.of(offsetString);
String formattedTimestamp = dateTime.atOffset(offset).toString();
System.out.println(formattedTimestamp);
This prints 此打印
2016-01-14T19:37:36-08:00
Stay away from outdated classes like SimpleDateFormat
. 远离过时的类,例如
SimpleDateFormat
。
What is offsetString
is not present? 什么是
offsetString
不存在? I understand that in this case you want an offset of Z
for UTC. 我了解在这种情况下,您希望UTC的偏移量为
Z
For example like this: 例如这样:
ZoneOffset offset;
if (offsetString == null) {
offset = ZoneOffset.UTC;
} else {
offset = ZoneOffset.of(offsetString);
}
String formattedTimestamp = dateTime.atOffset(offset).toString();
With a null
offsetString
we now get 使用
null
offsetString
我们现在得到
2016-01-14T19:37:36Z
The classes in java.time
(of which I'm using but a few) are described in JSR-310 and come built-in with Java 8. What if you would like to use them with Java 6 or 7? JSR-310中描述了
java.time
的类(我正在使用其中的一些),这些类是Java 8内置的。如果您想在Java 6或7中使用它们,该怎么办? You get the ThreeTen Backport (link below). 您将获得ThreeTen Backport(下面的链接)。 It gives you the majority of the classes for Java 6 and 7. I'm not perfectly happy to tell you you need an external library, but in this case it's only until you move to Java 8. I hope you will soon.
它为您提供了Java 6和7的大多数类。我不太高兴告诉您您需要一个外部库,但是在这种情况下,只有在您移至Java 8之前,我希望您很快。
I am sure it can be done with JodaTime too, but I haven't got experience with it, so cannot give you the details there. 我确信它也可以用JodaTime完成,但是我没有经验,因此无法在此提供详细信息。 What I do know, I have read the the folks behind JodaTime now recommend you move over to
java.time
instead. 我所知道的是,我已经阅读过JodaTime背后的人们,现在建议您改为使用
java.time
。 So I am asking you to swap one external library for a newer (and supposedly better) one. 因此,我想请您将一个外部库换成一个新的(据称更好的)库。 In itself I'm not unhappy with that.
就其本身而言,我并不为此感到不满。 Only if you already have a codebase that uses JodaTime, it's not really trivial.
仅当您已经具有使用JodaTime的代码库时,它才是真正的琐事。
Link: ThreeTen Backport 链接: ThreeTen Backport
You can find more examples in section Examples at :- http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 您可以在以下示例部分中找到更多示例: -http : //docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
String string2 = "2001-07-04T12:08:56.235-07:00";
Date result2 = df2.parse(string2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.