Java 使用SimpleDataFormat格式化字符串分析异常?

Java 使用SimpleDataFormat格式化字符串分析异常?,java,date,simpledateformat,Java,Date,Simpledateformat,我有一个date对象,它在doingdate.toString() 我想将其格式设置为这种风格: "June-27-2018 5:33:00 PM GMT". 我尝试使用SimpleDateFormat protected SimpleDateFormat dateFormat = new SimpleDateFormat( "MMMM-dd-yyyy h:mm:ss a z", Locale.US); 但我一直收到一个解析异常。有没有办法按照我需要的方式格式化?时区也需要转换。您的日期字符

我有一个date对象,它在doing
date.toString()

我想将其格式设置为这种风格:

"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

若将日期或对象传递给format函数,您将获得所需的输出

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
类早已过时(没有双关语)。今天,您应该更喜欢使用
java.time
,这是一种现代的、更好的日期和时间API。但是,我假设您从一些无法更改的遗留API中获得了一个
日期
。您应该做的第一件事是将其转换为
即时
Instant
java.time
中的对应类。然后你应该在那里做进一步的操作

    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);
此代码段打印所需的:

2018年6月27日格林尼治标准时间下午5:33:00

直接从
Date
对象转换比从其字符串表示形式转换更安全、更容易。后者最大的问题是字符串包含
CDT
作为时区,这是不明确的。它可以代表澳大利亚中央夏令时、北美中央夏令时、古巴夏令时或查塔姆夏令时。您无法确定Java给您的是哪一个。如果有任何方法可以避免的话,千万不要依赖三个和四个字母的时区缩写


链接:解释如何使用
java.time

我明白了。如何将时区更改为GMT?非常好!我讨厌我们必须使用两个dateFormat实例。但是哦,好吧:(当您应该使用
格式
方法进行格式化时,听起来好像您在尝试
解析
方法?我建议您避免使用
SimpleDateFormat
Date
类。它们不仅过时很久,
SimpleDateFormat
也是出了名的麻烦。今天,我们在这方面做得更好。欢迎来到Stack Overflow!您的问题非常适合。所以请?请在发布之前彻底搜索Stack Overflow。解析日期时间字符串的格式模式问题已经讨论过了。
    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);