在Java 7中格式化ISO 8601 [英] Formatting ISO 8601 in Java 7

查看:147
本文介绍了在Java 7中格式化ISO 8601的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的日期格式 2016-11-25T09:29:10.588 + 01:00

我想将其转换为毫秒,然后在打印时再将毫秒转换为 yyyy-MM-dd HH:mm:ss.SSS 这种格式。

I want to convert this to milliseconds and later while printing I have to again convert milliseconds to "yyyy-MM-dd HH:mm:ss.SSS" this format.

static FastDateFormat alarmDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS");
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSXXX");
df1.setTimeZone(TimeZone.getTimeZone("UTC"));
String string1 = "2016-11-25T09:29:10.588+01:00";
Date result1 = df1.parse(string1);
long timeStamp = result1.getTime();
System.out.println("timeStamp : " +timeStamp);
String eventTime = alarmDateFormat.format(timeStamp);
System.out.println("AfterConverting : " + eventTime);

结果:

timeStamp : 1480062550588
AfterConverting : 2016-11-25 13:59:10.588

日期是正确的,但时间有所不同。我尝试将时区设置为 UTC,但无济于事。

Date is proper but the time is different. I have tried setting timezone to "UTC" but it did't help.

请注意:在Java 8中,它可以与其他库(如Instant和其他所有库)一起正常工作,但我们想要在Java 7中。

Please note: In Java 8 it works fine with other libraries like Instant and all but we wanted in Java 7.

推荐答案

您在<$中调用 setTimeZone c $ c> df1 ,但是此格式化程序仅用于解析,并且对格式化没有任何影响。

You're calling setTimeZone in df1, but this formatter is used only for parsing, and it won't have any effect in formatting.

要格式化日期设置为特定时区,则必须在另一个格式化程序中进行设置(如果未设置,则格式化程序将使用JVM默认时区)。另外,您不需要依赖其他类,例如 FastDateFormat ,只需使用另一个 SimpleDateFormat

To format the date to a specific timezone, you must set it in the other formatter (if you don't set, the formatter uses the JVM default timezone). Also, you don't need to rely on another class such as FastDateFormat, you can simply use another SimpleDateFormat.

但是要获得完全相同的日期/时间,您需要使用输入中使用的相同偏移量( +01:00 )。 日期对象不要保留此信息,因此您必须从输入中提取出来。一种方法是使用子字符串

But to get exactly the same date/time, you need to use the same offset used in the input (+01:00). Date objects don't keep this information, so you must extract it from the input. One way would be to use substring:

SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSXXX");
String string1 = "2016-11-25T09:29:10.588+01:00";
Date result1 = df1.parse(string1);
long timeStamp = result1.getTime();
System.out.println("timeStamp : " + timeStamp);

// extract the +01:00 offset from the input
String offset = string1.substring(23);
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
// set the offset in the formatter (so output is converted to it)
df2.setTimeZone(TimeZone.getTimeZone("GMT" + offset));
String eventTime = df2.format(timeStamp);
System.out.println("AfterConverting : " + eventTime);

输出将是:


timeStamp:1480062550588

AfterConverting:2016-11-25 09:29:10.588

timeStamp : 1480062550588
AfterConverting : 2016-11-25 09:29:10.588

如果您想继续使用 FastDateFormat ,还可以在其中设置时区:

If you want to keep using FastDateFormat, it's also possible to set the timezone in it:

FastDateFormat f = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS", TimeZone.getTimeZone("GMT" + offset));
String eventTime = f.format(timeStamp);






Java新的日期/时间API



旧类(日期日历 SimpleDateFormat )有很多问题设计问题,并且已被新的API取代。


Java new Date/Time API

The old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues, and they're being replaced by the new APIs.

对于 Java 7 strong>,您可以使用 ThreeTen Backport ,它是Java 8的新日期/时间类的绝佳反向端口。对于 Android ,您还需要 ThreeTenABP (有关如何操作的更多信息在此处)使用它。

For Java 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, you'll also need the ThreeTenABP (more on how to use it here).

有了这个,您可以将输入解析为 org.threeten.bp.OffsetDateTime 并使用 org.threeten.bp.format.DateTimeFormatter 用于输出。在这种情况下, OffsetDateTime 保留偏移信息,因此您无需在格式化程序中进行设置:

With this, you can parse the input to a org.threeten.bp.OffsetDateTime and use a org.threeten.bp.format.DateTimeFormatter for the output. In this case, the OffsetDateTime keeps the offset information, so you don't need to set it in the formatter:

OffsetDateTime odt = OffsetDateTime.parse(string1);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println("timeStamp : " + odt.toInstant().toEpochMilli());
System.out.println("AfterConverting : " + odt.format(fmt));

输出是相同的:


timeStamp:1480062550588

AfterConverting:2016-11-25 09:29:10.588

timeStamp : 1480062550588
AfterConverting : 2016-11-25 09:29:10.588

这篇关于在Java 7中格式化ISO 8601的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆