【问题标题】:Special string to LocalDateLocalDate 的特殊字符串
【发布时间】:2019-12-17 08:36:45
【问题描述】:

我想从下面的字符串中获取 LocalDate 或 LocalDateTime。

2019 年 1 月 1 日 12:00:00 AM

我已经尝试了以下代码,但出现以下错误:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d, yyyy hh:mm:ss a"); // also with dd
formatter = formatter.withLocale(Locale.GERMAN);
LocalDateTime localDateTime = LocalDateTime.parse(stringDate, formatter);
LocalDate localDate = LocalDate.parse(stringDate, formatter);
  • Exception in thread "main" java.time.format.DateTimeParseException: Text 'Jan 1, 2019 12:00:00 AM' could not be parsed at index 0

我想要的日期最终会是这样的

  • 01.01.2019。本地日期
  • 01.01.2019。 12:00 本地日期时间(24 小时)

这甚至可能吗?相反,使用正则表达式会不会有点过头了?

【问题讨论】:

  • “哪个不工作” - 你能详细说明一下吗?
  • 我的错。我收到以下错误:线程“main”中的异常 java.time.format.DateTimeParseException: Text 'Jan 1, 2019 12:00:00 AM' could not be parsed at index 0
  • 我会抛出同样的异常,但如果我删除 formatter = formatter.withLocale(Locale.GERMAN); 则不会(我假设我的 Locale 默认为 US)。
  • @JacobG WFM。但我使用的是 Java 8。您使用的是未来版本吗? 9 中的一些语言环境更改并不完全向后兼容 IIRC。
  • @MichaelBerry 我正在使用 Java 12 对其进行测试,所以这是可能的。

标签: java date localdate


【解决方案1】:

您收到错误是因为您尝试将 LocalDateTime 格式解析为 LocalDate。

如果您有完整的 LocalDateTime,则不需要单独的 LocalDate。如果有需要,LocalDateTime 中还有一个 toLocalDate() 方法。

    String stringDate = "Jan 1, 2019 12:00:00 AM";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d, yyyy hh:mm:ss a"); // also with dd
    formatter = formatter.withLocale(Locale.GERMAN);
    LocalDateTime localDateTime = LocalDateTime.parse(stringDate, formatter);

    DateTimeFormatter firstOutputFormat = DateTimeFormatter.ofPattern("MM.DD.YY.");
    String outputFirst = localDateTime.format(firstOutputFormat).concat(" LocalDate");

    DateTimeFormatter secondOutputFormat = DateTimeFormatter.ofPattern("MM.DD.YY. HH.mm");
    String outputSecond = localDateTime.format(secondOutputFormat).concat(" LocalDateTime (24h)");

【讨论】:

【解决方案2】:

您的代码存在一些问题,但我会在这里提出一个可行的解决方案:

  1. HH 是一天中的小时,而不是 0-12 小时(即 hh
  2. 您应该使用 DateTimeFormatterBuilder
  3. 您应该使用正确的语言环境,可能是GERMANY 而不是GERMAN
  4. 月份的名称表示为L,而不是M
  5. 德语语言环境使用vorm.nachm. 而不是AMPM -> 一个快速的解决方案是替换术语

把它们放在一起,我们就得到了这样的结果:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

class Scratch {

    public static void main(String[] args) {
        String stringDate = "Jan 1, 2019 11:00:00 PM";
        stringDate = stringDate.replace("AM", "vorm.");
        stringDate = stringDate.replace("PM", "nachm.");
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .appendPattern("LLL d, yyyy hh:mm:ss a")
                .toFormatter(Locale.GERMANY);
        LocalDateTime localDateTime = LocalDateTime.parse(stringDate, formatter);
        LocalDate localDate = LocalDate.parse(stringDate, formatter);
    }

}

如果有人有不同的方法来处理AM/vorm.-dilemma,我会很高兴看到它!

【讨论】:

  • 无论使用何种 jdk 都有效。结合@ejdebruin DateTimeFormatter firstOutputFormat = DateTimeFormatter.ofPattern("MM.DD.YY."); String outputFirst = localDateTime.format(firstOutputFormat).concat(" LocalDate"); 的输入,我从一开始就得到了我想要的。
  • 由于某种原因,这不适用于以下字符串 String stringDate = "May 1, 2019 12:00:00 AM"; 我在这部分 LocalDateTime localDateTime = LocalDateTime.parse(stringDate, formatter); 收到以下错误 Exception in thread "main" java.time.format.DateTimeParseException: Text 'May 1, 2019 12:00:00 vorm.' could not be parsed at index 0
  • 知道了。正如你已经提到的,一切都必须翻译成德语。示例:if (stringDate.contains("May")) { stringDate = stringDate.replace("May","Mai"); }
  • 嘿,我刚看了你的cmets。当然Feb 会起作用,因为它是英文的february 和德文的FebruarMay (may vs Mai) 会出现问题。您将语言环境设置为德语,这就是为什么我认为您正在解析德语 Januar。不过,您可以将语言环境设置为英语,然后您甚至不必翻译 AM/PM-part。给我一个快速的回复,如果有必要我会更正我的答案。
  • 您的回答非常好,不用担心。不过,我也用.toFormatter(Locale.ENGLISH); 对其进行了测试,删除stringDate = stringDate.replace("AM", "vorm."); stringDate = stringDate.replace("PM", "nachm."); 后它也可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 2012-02-03
  • 2011-09-18
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多