使用 SimpleDateFormat 格式化字符串解析异常?
Using SimpleDateFormat to format a string parse exception?
我有一个日期对象 returns 下面的字符串值 date.toString()
String date = "Wed Jun 27 12:33:00 CDT 2018";
我想完全按照这种风格格式化它:
"June-27-2018 5:33:00 PM GMT".
我尝试使用 SimpleDateFormat
protected SimpleDateFormat dateFormat = new SimpleDateFormat( "MMMM-dd-yyyy h:mm:ss a z", Locale.US);
但我一直收到解析异常。有什么办法可以按照我需要的方式对其进行格式化吗?时区也需要转换。
您的日期字符串无法解析为您提供的格式,因此请将格式更改为 EEE MMM dd HH:mm:ss zzz yyyy
String myDate = "Wed Jun 27 12:33:00 CDT 2018";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
SimpleDateFormat dateFormat_2 = new SimpleDateFormat("MMMM-dd-yyyy h:mm:ss a z", Locale.US);
dateFormat_2.setTimeZone(TimeZone.getTimeZone("GMT"));
Date d = dateFormat.parse(myDate);
dateFormat_2.format(d);
System.out.println(dateFormat_2.format(d));
输出:
June-27-2018 12:33:00 PM GMT
如果将日期或对象传递给格式化函数,您将获得所需的输出。
SimpleDateFormat dateFormat = new SimpleDateFormat( "MMMM-dd-yyyy h:mm:ss a z", Locale.US);
String ans=dateFormat.format(param);
在上面的代码中,param
必须是日期或对象,因此首先将字符串转换为日期,然后应用格式函数以获得所需的输出。
见下方示例代码
SimpleDateFormat dateFormat = new SimpleDateFormat( "MMMM-dd-yyyy h:mm:ss a z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String ans=dateFormat.format(new Date());
示例输出:
June-27-2018 6:22:35 PM GMT
首先,你不应该有一个 Date
对象。 Date
class 早就过时了(没有双关语意)。今天您应该更喜欢使用 java.time
,现代且更好的日期和时间 API。但是,我假设您从一些无法更改的遗留 API 中获得了 Date
。您应该做的第一件事是将其转换为 Instant
。 Instant
是java.time
中对应的class。然后你应该从那里做任何进一步的操作。
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("MMMM-dd-yyyy h:mm:ss a z", Locale.US);
ZoneId desireedZone = ZoneId.of("Etc/GMT");
Date yourOldfashionedDate = // …;
ZonedDateTime dateTimeInGmt = yourOldfashionedDate.toInstant().atZone(desireedZone);
String formattedDateTime = dateTimeInGmt.format(formatter);
System.out.println(formattedDateTime);
此代码段打印所需内容:
June-27-2018 5:33:00 PM GMT
直接从 Date
对象转换比从其字符串表示形式转换更安全、更容易。后者最大的问题是字符串包含 CDT
作为时区,这是不明确的。它可能代表澳大利亚中部夏令时、北美中部夏令时、古巴夏令时或查塔姆夏令时。您无法确定 Java 给您的是哪一个。如果有任何方法可以避免,切勿依赖三字母和四字母时区缩写。
Link: Oracle tutorial: Date Time 解释如何使用 java.time
.
我有一个日期对象 returns 下面的字符串值 date.toString()
String date = "Wed Jun 27 12:33:00 CDT 2018";
我想完全按照这种风格格式化它:
"June-27-2018 5:33:00 PM GMT".
我尝试使用 SimpleDateFormat
protected SimpleDateFormat dateFormat = new SimpleDateFormat( "MMMM-dd-yyyy h:mm:ss a z", Locale.US);
但我一直收到解析异常。有什么办法可以按照我需要的方式对其进行格式化吗?时区也需要转换。
您的日期字符串无法解析为您提供的格式,因此请将格式更改为 EEE MMM dd HH:mm:ss zzz yyyy
String myDate = "Wed Jun 27 12:33:00 CDT 2018";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
SimpleDateFormat dateFormat_2 = new SimpleDateFormat("MMMM-dd-yyyy h:mm:ss a z", Locale.US);
dateFormat_2.setTimeZone(TimeZone.getTimeZone("GMT"));
Date d = dateFormat.parse(myDate);
dateFormat_2.format(d);
System.out.println(dateFormat_2.format(d));
输出:
June-27-2018 12:33:00 PM GMT
如果将日期或对象传递给格式化函数,您将获得所需的输出。
SimpleDateFormat dateFormat = new SimpleDateFormat( "MMMM-dd-yyyy h:mm:ss a z", Locale.US);
String ans=dateFormat.format(param);
在上面的代码中,param
必须是日期或对象,因此首先将字符串转换为日期,然后应用格式函数以获得所需的输出。
见下方示例代码
SimpleDateFormat dateFormat = new SimpleDateFormat( "MMMM-dd-yyyy h:mm:ss a z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String ans=dateFormat.format(new Date());
示例输出:
June-27-2018 6:22:35 PM GMT
首先,你不应该有一个 Date
对象。 Date
class 早就过时了(没有双关语意)。今天您应该更喜欢使用 java.time
,现代且更好的日期和时间 API。但是,我假设您从一些无法更改的遗留 API 中获得了 Date
。您应该做的第一件事是将其转换为 Instant
。 Instant
是java.time
中对应的class。然后你应该从那里做任何进一步的操作。
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("MMMM-dd-yyyy h:mm:ss a z", Locale.US);
ZoneId desireedZone = ZoneId.of("Etc/GMT");
Date yourOldfashionedDate = // …;
ZonedDateTime dateTimeInGmt = yourOldfashionedDate.toInstant().atZone(desireedZone);
String formattedDateTime = dateTimeInGmt.format(formatter);
System.out.println(formattedDateTime);
此代码段打印所需内容:
June-27-2018 5:33:00 PM GMT
直接从 Date
对象转换比从其字符串表示形式转换更安全、更容易。后者最大的问题是字符串包含 CDT
作为时区,这是不明确的。它可能代表澳大利亚中部夏令时、北美中部夏令时、古巴夏令时或查塔姆夏令时。您无法确定 Java 给您的是哪一个。如果有任何方法可以避免,切勿依赖三字母和四字母时区缩写。
Link: Oracle tutorial: Date Time 解释如何使用 java.time
.