这究竟是什么&QUOT时间格式,日期和QUOT;:"二〇一四年八月二十○日00:00:00 -0500"?二十、四年、日期、格式

2023-09-08 09:06:30 作者:迷毁少年

我试图将这个日期的方式如下:

 的SimpleDateFormat fromFormat =新的SimpleDateFormat(YYYY-MM-DD HH:MM:SS SSSZ);
 

但我得到了:

  java.text.ParseException:无法解析的日期:2014年9月20日00:00:00 -0500(偏移量20)
 

解决方案 时间不多了,请立即查收这份详尽的 x26quot A级 x26quot 攻略

这是-0500是UTC的偏移量,在RFC822格式。你只是想以Z ,没有 SSS

借助的Andr​​oid 的SimpleDateFormat 文档有像这样的表:

符号:Z 含义:时区(RFC 822) 类:(时区) 示例: Z / ZZ / ZZZ : - 0800 ZZZZ :格林尼治标准​​时间08:00 ZZZZZ : - 08:00

我也亲自指定一个区域,作为理所当然的事:这是一个机器可读的格式,而不是一个人性化的格式,所以我通常会指定 Locale.US

  SimpleDateFormat的格式=新的SimpleDateFormat(YYYY-MM-DD HH:MM:SS Z,
                                                Locale.US);
字符串文本=2014年8月20号00:00:00 -0500;
的System.out.println(format.parse(文本));
 

I tried converting this date the following way:

SimpleDateFormat fromFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSSZ");

but I got:

 java.text.ParseException: Unparseable date: "2014-09-20 00:00:00 -0500" (at offset 20)

解决方案

That "-0500" is the offset from UTC, in RFC822 format. You just want Z, without the SSS.

The Android SimpleDateFormat docs have it like this in the table:

Symbol: Z Meaning: time zone (RFC 822) Kind: (Time Zone) Example: Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00

I would also personally specify a locale, as a matter of course: this is a machine-readable format rather than a human-oriented format, so I'd usually specify Locale.US:

SimpleDateFormat format  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z",
                                                Locale.US);
String text = "2014-08-20 00:00:00 -0500";
System.out.println(format.parse(text));