SimpleDateFormat无法解析带有偏移值(如’GMT-0500’)的日期。

huangapple go评论70阅读模式
英文:

SimpleDateFormat is not able to parse the date with offset value like - 'GMT-0500'

问题

以下是翻译好的内容:

我正在尝试解析这个(和许多类似的)日期字符串 - "Wed Aug 26 2020 11:03:30 GMT-0500"

从SimpleDateFormat的文档中看,我以为这样的模式应该可以工作:

String dateFormat = "EEE MMM d yyyy HH:mm:ss z";

然而,实际并不是这样。但是以下格式可以解析:

String dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'z";

但是当我打印解析后的日期时,我得到的时间加了一个小时,并且偏移减少了一个小时 - Wed Aug 26 12:03:30 GMT-04:00 2020

我该怎么做才能防止这种偏移变化?

以下是示例代码:

String dateStr = "Wed Aug 26 2020 11:03:30 GMT-0500";        
String dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'z";
Date date = new SimpleDateFormat(dateFormat).parse(dateStr);
System.out.println("原始日期字符串:" + dateStr);
System.out.println("原始日期对象:" + date);
输出
原始日期字符串Wed Aug 26 2020 11:03:30 GMT-0500
原始日期对象Wed Aug 26 12:03:30 GMT-04:00 2020
英文:

I am trying to parse this (and many similar) dateString - "Wed Aug 26 2020 11:03:30 GMT-0500"

Looking at the SimpleDateFormat documentation, I was assuming that a pattern like this should work:

String dateFormat = "EEE MMM d yyyy HH:mm:ss z";

However, it doesn't. But the following format is able to parse

String dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'z";

But when I print the parsed date, I get the time with an hour added and offset reduced by an hour - Wed Aug 26 12:03:30 GMT-04:00 2020

What can I do to prevent this offset change?

Here is the sample code:

String dateStr = "Wed Aug 26 2020 11:03:30 GMT-0500";        
String dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'z";
Date date = new SimpleDateFormat(dateFormat).parse(dateStr);
System.out.println("Original Date String : "+dateStr);
System.out.println("Original Date  Object : "+date);
Output:
Original Date String : Wed Aug 26 2020 11:03:30 GMT-0500
Original Date  Object : Wed Aug 26 12:03:30 GMT-04:00 2020

答案1

得分: 2

使用 java.time.OffsetDateTime 在这里,因为该字符串中没有时区,只有一个偏移量,而您正在使用的类已经过时,原因是合理的... 摆脱 java.util.Datejava.text.SimpleDateFormat

看这个例子:

public static void main(String[] args) {
	// 提供要解析的字符串
	String dateStr = "Wed Aug 26 2020 11:03:30 GMT-0500";
	// 提供匹配的模式
	String dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'Z";
	// 使用这个模式和适当的语言环境创建一个格式化程序
	DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat, Locale.ENGLISH);
	// 使用格式化程序将字符串解析为 OffsetDateTime
	OffsetDateTime odt = OffsetDateTime.parse(dateStr, dtf);
	// 以默认格式打印结果
	System.out.println("默认/ISO 格式:\t" + odt);
	// 并以您的自定义格式打印它
	System.out.println("自定义格式:\t\t" + odt.format(dtf));
}

输出:

默认/ISO 格式:	2020-08-26T11:03:30-05:00
自定义格式:		Wed Aug 26 2020 11:03:30 GMT-0500
英文:

Use java.time.OffsetDateTime here because there is no zone in that String, just an offset and the classes you are using are outdated for good reasons... Get rid of java.util.Date and java.text.SimpleDateFormat.

See this example:

public static void main(String[] args) {
	// provide the String to be parsed
	String dateStr = "Wed Aug 26 2020 11:03:30 GMT-0500";
	// provide a matching pattern
	String dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'Z";
	// create a formatter with this pattern and a suitable locale for unit names
	DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat, Locale.ENGLISH);
	// parse the String to an OffsetDateTime using the formatter
	OffsetDateTime odt = OffsetDateTime.parse(dateStr, dtf);
	// print the result in the default format
	System.out.println("Default/ISO format:\t" + odt);
	// and print it in your custom format
	System.out.println("Custom format:\t\t" + odt.format(dtf));
}

Output:

Default/ISO format:	2020-08-26T11:03:30-05:00
Custom format:		Wed Aug 26 2020 11:03:30 GMT-0500

huangapple
  • 本文由 发表于 2020年8月25日 22:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63580799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定