【问题标题】:How to parse String to Date in Java如何在 Java 中将字符串解析为日期
【发布时间】:2020-06-01 12:20:19
【问题描述】:

我想将字符串24 May 2020 07:40 AM 转换为日期格式Mon May 24 07:40:55 IST 2020。我尝试使用 Calendar 和 SimpleDateFormatter 但没有找到解决方案。任何帮助表示赞赏。

我希望返回类型为 Date,因为我必须将它与几个 Dates 进行比较。

【问题讨论】:

  • 我建议你不要使用SimpleDateFormatCalendar。这些类设计不良且过时,尤其是前者,尤其是出了名的麻烦。而是使用来自java.time, the modern Java date and time APILocalDateTimeDateTimeFormatter
  • :55IST 不在原始字符串中时,您希望它们来自哪里?还是应该一直是:55IST
  • LocalDateTime.parse("24 May 2020 07:40 AM", DateTimeFormatter.ofPattern("d MMM uuuu hh:mm a", Locale.ENGLISH)).atZone(ZoneId.of("Europe/Dublin")).format(DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH)) 产生Sun May 24 07:40:00 IST 2020。请分解成更小的陈述。请注意,多个时区可以打印为IST,因此请选择适合您的时区。
  • 欢迎来到 Stack Overflow。当您尝试了某事但没有成功时,如果您在问题中发布您的尝试以及观察到的问题(例如错误消息),您通常会发现这里的人承认您的努力并且更加友好并且更愿意提供帮助。此外,当我们发现您出了什么问题时,我们可以更好地了解您的情况并写出更好的答案。
  • @OleV.V.您的解决方案是以所需格式打印所需的日期,但我希望返回类型为日期,因为我必须将它与几个日期进行比较。您的解决方案是以字符串格式返回它。

标签: java date datetime date-format date-formatting


【解决方案1】:

java.time

当你有一些Date 对象时——可能来自一个你现在无法升级到 java.time 的遗留 API——我仍然建议你使用 java.time,现代 Java 日期和时间 API,供您比较。

在以下示例中,我使用来自 java.time 的 Instant,但您也可以使用 ZonedDateTime 或其他一些现代类型。

    DateTimeFormatter fromFormatter = DateTimeFormatter.ofPattern("d MMM uuuu hh:mm a", Locale.ENGLISH);

    Date anOldfashionedDate = new Date(1_590_286_000_000L);
    Date anotherOldfashionedDate = new Date(1_590_287_000_000L);
    System.out.println("The Date objects are " + anOldfashionedDate + " and " + anotherOldfashionedDate);

    String aString = "24 May 2020 07:40 AM";

    Instant instantFromDate = anOldfashionedDate.toInstant();
    Instant instantFromAnotherDate = anotherOldfashionedDate.toInstant();
    Instant instantFromString = LocalDateTime.parse(aString, fromFormatter)
            .atZone(ZoneId.of("Asia/Kolkata"))
            .toInstant();

    System.out.println("Comparing " + instantFromDate + " and " + instantFromString + ": "
            + instantFromDate.compareTo(instantFromString));
    System.out.println("Comparing " + instantFromAnotherDate + " and " + instantFromString + ": "
            + instantFromAnotherDate.compareTo(instantFromString));

输出是(在亚洲/加尔各答时区运行时):

The Date objects are Sun May 24 07:36:40 IST 2020 and Sun May 24 07:53:20 IST 2020
Comparing 2020-05-24T02:06:40Z and 2020-05-24T02:10:00Z: -1
Comparing 2020-05-24T02:23:20Z and 2020-05-24T02:10:00Z: 1

Instant 以 UTC 打印;这是它的toString 方法生成的。尾随 Z 表示 UTC。由于印度标准时间比 UTC 时间早 5 小时 30 分钟,因此印度的 07:40 AM 与 UTC 的 02:10 时间相同。

鉴于您现在开始使用 java.time,当您的旧 API 也升级为使用 java.time 时,您已做好充分准备。

相反的转换

如果您确实坚持使用Date,按要求回答您的问题,则相反的转换也很容易:

    Date oldfashionedDateFromInstantFromString = Date.from(instantFromString);
    System.out.println("Converting to old-fashioned: " + oldfashionedDateFromInstantFromString);

转换为老式:2020 年 5 月 24 日星期日 07:40:00 IST

链接

Oracle tutorial: Date Time 解释如何使用 java.time。

【讨论】:

  • 天哪,你怎么总是有时间为java.time写出高质量的答案? +1
猜你喜欢
  • 2012-07-11
  • 2013-06-05
  • 2012-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 2018-02-27
  • 1970-01-01
相关资源
最近更新 更多