SimpleDateFormat 正在解析为不同的日期

SimpleDateFormat is parsing to a different Date

我正在尝试使用 SimpleDateFormat 将字符串解析为日期,但它生成了错误的日期值。

String dataNascimentoValue = "Thu Jan 01 00:00:00 GMT 1970";
SimpleDateFormat converter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");

Date date = new Date();

converter.setLenient(false);
date = converter.parse( dataNascimentoValue ); //at this moment It generates //a wrong Date: Wed Dec 31 19:00:00 EST 1969

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String formatedDate = formatter.format( date );

我已经尝试在我的 SimpleDateFormat 中放置一个区域设置,但没有成功。 我想将字符串解析为日期,因为我需要在之后对其进行格式化。

有什么帮助吗?

Screenshot of my try using the Locale.US

您可以使用此方法解析字符串到日期。

 private static final SimpleDateFormat _simpleDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

public static Date stringToJavaDateOnly(String dateStr)
            throws ParseException {
        return _simpleDateTimeFormat.parse(dateStr);
    }

tl;博士

ZonedDateTime.parse ( 
    "Thu Jan 01 00:00:00 GMT 1970" , 
    DateTimeFormatter.ofPattern ( "EEE MMM dd HH:mm:ss zzz uuuu" , Locale.CANADA )                    
)

功能,不是错误

这两个字符串:

  • Thur Jan 01 00:00:00 GMT 1970
  • Wed Dec 31 19:00:00 EST 1969

…代表非常相同的时刻,时间轴上的相同点。所以这里没问题;您看到的是记录在案的正确行为。

EST 指的是美国和加拿大东部大部分地区使用的时区。这些区域比 UTC (GMT) 晚五个小时。前一天的午夜减去五小时是晚上 7 点(19:00)。不同 wall-clock time,但宇宙历史上的同一时刻。

遗留日期时间 class 中许多糟糕的设计选择之一,例如 Date 是在调用 JVM 期间动态应用 JVM 的当前默认时区 toString 生成一个值的表示,否则它总是在 UTC 中。非常令人困惑,因为这种行为会产生时区错觉,而实际上 Date 始终采用 UTC。更令人困惑的是,实际上 一个位于 Date 深处的时区,用于某些内部目的,但该时区没有任何 getter 或 setter . class 真是一团糟。避开它。

根据您的报告,我可以推断出您的 JVM 当前的默认时区是 EST。

使用java.time

您使用的是麻烦的旧日期时间 classes,现在已被 java.time classes 取代。避免这些遗留 classes;仅使用 java.time classes.

在解析日期时间字符串时始终指定 Locale。如果省略,您的 JVM 当前默认值 Locale 用于 (a) 确定用于翻译文本的人类语言,例如英语,以及 (b) 确定决定缩写、大写、标点符号等问题的文化规范。

String input = "Thu Jan 01 00:00:00 GMT 1970";
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "EEE MMM dd HH:mm:ss zzz uuuu" , Locale.CANADA );
ZonedDateTime zdt = ZonedDateTime.parse ( input , f );

zdt.toString(): 1970-01-01T00:00Z[GMT]

您可以调整到其他时区。应用 ZoneId to get another ZonedDateTime.

指定一个proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca,或Pacific/Auckland。切勿使用 3-4 个字母的缩写,例如 ESTIST,因为它们 不是 真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdtMontreal = zdt.withZoneSameInstant( z );

zdtMontreal.toString(): 1969-12-31T19:00-05:00[America/Montreal]

java.time classes 上的 toString 方法更明智和清晰,在生成日期时间值的文本表示时使用标准 ISO 8601 格式。

参见 live code at IdeOne.com


关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

从哪里获得java.time classes?

  • Java SE 8 and SE 9 及更高版本
    • 内置。
    • 标准 Java API 的一部分,带有捆绑实施。
    • Java 9 添加了一些小功能和修复。
  • Java SE 6 and SE 7
  • Android
    • ThreeTenABP 项目专门为 Android 改编 ThreeTen-Backport(如上所述)。
    • 参见

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.