Java将ISO 8601字符串转换为日期忽略偏移量
我收到一个字符串,格式如下:“yyyy-MM-dd'T'HH:MM:ssZ”ex:2020-09-09T09:58:00+0000”偏移量为UTC 我需要在不应用偏移量“+0000”的情况下将字符串转换为日期对象,但在运行代码时,我会得到不同的时间:Java将ISO 8601字符串转换为日期忽略偏移量,java,date,jodatime,iso8601,Java,Date,Jodatime,Iso8601,我收到一个字符串,格式如下:“yyyy-MM-dd'T'HH:MM:ssZ”ex:2020-09-09T09:58:00+0000”偏移量为UTC 我需要在不应用偏移量“+0000”的情况下将字符串转换为日期对象,但在运行代码时,我会得到不同的时间: DateTimeFormatter isoFormat = ISODateTimeFormat.dateTimeParser(); Date date = isoFormat.parseDateTime("2020-09-09T09:58
DateTimeFormatter isoFormat = ISODateTimeFormat.dateTimeParser();
Date date = isoFormat.parseDateTime("2020-09-09T09:58:00+0000").toDate();
// Wed Sep 09 05:58:00 EDT 2020
如上所示,日期已更改
相反,我希望保留相同的日期和时间,比如:Wed Sep 09 09:58:00,这样我就可以将这个日期对象转换为一个字符串,分别使用“yyyy-MM-dd”、“HH:MM:ss”和“yyyy-MM-dd'T'HH:MM:ss”格式。我建议您使用
java.time
date-time API和相应的格式化API(软件包,java.time.format
)。从了解有关现代日期时间API的更多信息
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// The given date-time string
String strDateTime = "2020-09-09T09:58:00+0000";
// Define the formatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssZ");
// Parse the given date-time string into OffsetDateTime
OffsetDateTime odt = OffsetDateTime.parse(strDateTime, formatter);
// Output OffsetDateTime in the default format
System.out.println(odt);
// Print OffsetDateTime using the defined formatter
String formatted = formatter.format(odt);
System.out.println(formatted);
}
}
输出:
2020-09-09T09:58Z
2020-09-09T09:58:00+0000
2020-09-09T10:58:00.000+01:00
2020-09-09T09:58:00+0000
注意:java.util.Date
不表示日期/时间对象。它只表示从1970-01-01T00:00:00Z
的纪元开始的毫秒数。它没有任何时区或区域偏移信息。打印它时,java打印通过应用JVM的时区获得的字符串。我建议您停止使用java.util.Date
并切换到现代日期时间API
使用joda日期时间API,您可以按如下方式执行:
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
public class Main {
public static void main(String[] args) {
// The given date-time string
String strDateTime = "2020-09-09T09:58:00+0000";
// Define the formatter
DateTimeFormatter isoFormat = ISODateTimeFormat.dateTimeParser();
DateTime dateTime = isoFormat.parseDateTime("2020-09-09T09:58:00+0000");
// Display DateTime in the default format
System.out.println(dateTime);
// Define formatter for ouput
DateTimeFormatter outputFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").withZoneUTC();
// Display DateTime in the defined output format
String formatted = outputFormat.print(dateTime);
System.out.println(formatted);
}
}
输出:
2020-09-09T09:58Z
2020-09-09T09:58:00+0000
2020-09-09T10:58:00.000+01:00
2020-09-09T09:58:00+0000
我建议您使用
java.time
date-time API和相应的格式化API(package,java.time.format
)来实现。从了解有关现代日期-时间API的更多信息
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// The given date-time string
String strDateTime = "2020-09-09T09:58:00+0000";
// Define the formatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssZ");
// Parse the given date-time string into OffsetDateTime
OffsetDateTime odt = OffsetDateTime.parse(strDateTime, formatter);
// Output OffsetDateTime in the default format
System.out.println(odt);
// Print OffsetDateTime using the defined formatter
String formatted = formatter.format(odt);
System.out.println(formatted);
}
}
输出:
2020-09-09T09:58Z
2020-09-09T09:58:00+0000
2020-09-09T10:58:00.000+01:00
2020-09-09T09:58:00+0000
注意:java.util.Date
不表示日期/时间对象。它只表示从1970-01-01T00:00:00Z
的纪元开始的毫秒数。它没有任何时区或区域偏移信息。打印它时,java打印通过应用JVM的时区获得的字符串。我建议您停止使用java.util.Date
并切换到现代日期时间API
使用joda日期时间API,您可以按如下方式执行:
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
public class Main {
public static void main(String[] args) {
// The given date-time string
String strDateTime = "2020-09-09T09:58:00+0000";
// Define the formatter
DateTimeFormatter isoFormat = ISODateTimeFormat.dateTimeParser();
DateTime dateTime = isoFormat.parseDateTime("2020-09-09T09:58:00+0000");
// Display DateTime in the default format
System.out.println(dateTime);
// Define formatter for ouput
DateTimeFormatter outputFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").withZoneUTC();
// Display DateTime in the defined output format
String formatted = outputFormat.print(dateTime);
System.out.println(formatted);
}
}
输出:
2020-09-09T09:58Z
2020-09-09T09:58:00+0000
2020-09-09T10:58:00.000+01:00
2020-09-09T09:58:00+0000
答案的第一个也是最重要的部分是:不要转换成老式的
Date
。要么坚持使用Joda-Time,要么迁移到java.Time,这是现代java日期和时间API,正如Arvind Kumar Avinash的好答案中所述
由于您已经在使用Joda Time,我将向您展示一个Joda Time解决方案。说服格式化程序保留时间和偏移量的诀窍是withOffsetParsed()
但是!如果我正确猜测您希望以UTC(推荐做法)存储日期和时间,那么在解析器上指定UTC比withOffsetParsed()
更好:
DateTimeFormatter isoFormat
= ISODateTimeFormat.dateTimeParser().withZoneUTC();
现在,如果有一天出现UTC偏移量非零的字符串,您也将获得正确的时间
在任何情况下,我们现在都可以将您获得的DateTime
格式化为您请求的字符串
String dateString = dateTime.toString(ISODateTimeFormat.date());
System.out.println(dateString);
String timeString = dateTime.toString(ISODateTimeFormat.hourMinuteSecond());
System.out.println(timeString);
String dateTimeString = dateTime.toString(ISODateTimeFormat.dateHourMinuteSecond());
System.out.println(dateTimeString);
输出:
使用Date
有什么问题?首先,Date
类的设计很差,很早就过时了。其次,Date
只是一个时间点,它没有日期和时间的概念(他们尝试在Java 1.0中构建它,但在1997年放弃并在Java 1.1中弃用)。因此,日期
不能为您保留UTC中的日期和时间
代码中出现的情况是,您得到了一个表示正确时间点的
Date
。只有在打印该Date
时,您才隐式调用了它的toString
方法。Date.toString()
混淆地抓取JVM的时区设置(在您的例子中,显然是北美东部时间)并使用它来呈现要返回的字符串。因此,在您的例子中,时间点被呈现为Wed Sep 09 05:58:00 EDT 2020
答案的第一个也是最重要的部分是:不要转换为老式的日期
。要么坚持使用Joda time,要么迁移到java.time,即现代java日期和时间API阿文德·库马尔·阿维纳什(Arvind Kumar Avinash)已经给出了正确的答案
由于您已经在使用Joda Time,我将向您展示一个Joda Time解决方案。说服格式化程序保留时间和偏移量的诀窍是withOffsetParsed()
但是!如果我正确猜测您希望以UTC(推荐做法)存储日期和时间,那么在解析器上指定UTC比withOffsetParsed()
更好:
DateTimeFormatter isoFormat
= ISODateTimeFormat.dateTimeParser().withZoneUTC();
现在,如果有一天出现UTC偏移量非零的字符串,您也将获得正确的时间
在任何情况下,我们现在都可以将您获得的DateTime
格式化为您请求的字符串
String dateString = dateTime.toString(ISODateTimeFormat.date());
System.out.println(dateString);
String timeString = dateTime.toString(ISODateTimeFormat.hourMinuteSecond());
System.out.println(timeString);
String dateTimeString = dateTime.toString(ISODateTimeFormat.dateHourMinuteSecond());
System.out.println(dateTimeString);
输出:
使用Date
有什么问题?首先,Date
类的设计很差,很早就过时了。其次,Date
只是一个时间点,它没有日期和时间的概念(他们尝试在Java 1.0中构建它,但在1997年放弃并在Java 1.1中弃用)。因此,日期
不能为您保留UTC中的日期和时间
代码中出现的情况是,您得到了一个表示正确时间点的
Date
。只有在打印该Date
时,您才隐式调用了它的toString
方法。Date.toString()
混淆地抓取JVM的时区设置(在您的例子中,显然是北美东部时间)并使用它来呈现要返回的字符串。因此,在您的情况下,时间点被呈现为Wed Sep 09 05:58:00 EDT 2020
对不起,您的问题没有意义。如果不应用偏移量或时区,无法将字符串转换为日期。是否要应用*不同*偏移量ate发生了变化