【问题标题】:Java LocalDateTime parse error [duplicate]Java LocalDateTime 解析错误 [重复]
【发布时间】:2015-02-22 23:30:39
【问题描述】:

已经尝试了 4 个小时来解决这个问题。

:这行得通

String date = "Jul-01-2014 09:10:12";
LocalDateTime dt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("MMM-dd-yyyy HH:mm:ss", Locale.US));

:这不会

String date = "JUL-01-2014 09:10:12";
LocalDateTime dt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("MMM-dd-yyyy HH:mm:ss", Locale.US));

唯一的区别是月份全部大写。 Jul 工作的正确案例。 JUL 或 jul 都不起作用。我也尝试了“LLL”的模式,但没有运气。我错过了什么??

【问题讨论】:

  • Java 区分大小写
  • 詹姆斯的答案是正确的。使用 DateTimeFormatterBuilder.parseCaseInsensitive()

标签: java date java-time


【解决方案1】:

尝试使用 DateTimeFormatterBuilder 并使解析器不区分大小写。不要忘记指定语言环境。否则 MMM 在默认语言环境中可能无法解析月份缩写:

DateTimeFormatter format = new DateTimeFormatterBuilder()
  .parseCaseInsensitive()
  .appendPattern("dd-MMM-yyyy")
  .toFormatter(Locale.ENGLISH);

LocalDate.parse("03-jun-2015", format);

【讨论】:

  • .parseCaseInsensitive() 是我需要的。谢谢。
【解决方案2】:

显然我需要花 5 个小时在这上面。 在编写扩展程序以提供解决方法时,我发现了这一点。

    String date = "01-JUL-2014 09:10:12";

    DateTimeFormatterBuilder fmb = new DateTimeFormatterBuilder();
    fmb.parseCaseInsensitive();
    fmb.append(DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss"));

    LocalDateTime dt = LocalDateTime.parse(date, fmb.toFormatter());

适用于所有案例样式。

【讨论】:

  • 这应该被标记为接受的答案。
【解决方案3】:

official API 似乎不支持。

Symbol  Meaning                     Presentation      Examples
  ------  -------                     ------------      -------
   G       era                         text              AD; Anno Domini; A
   u       year                        year              2004; 04
   y       year-of-era                 year              2004; 04
   D       day-of-year                 number            189
   M/L     month-of-year               number/text       7; 07; Jul; July; J
   d       day-of-month                number            10

那里是唯一的月份选项,它没有明确提到任何支持三个大​​写字母月份的格式。

不过,将其转换回 Java 可以尊重的格式并不难;不过,它需要对日期进行一些修改并将其放回单个字符串中。

下面的解决方案不像使用第三方那样优雅或干净,但额外的好处是,人们根本不必依赖第三方库来获取此代码。

public String transformToNormalizedDateFormat(final String input) {
    String[] components = input.split("-");
    String month = components[0];
    if(month.length() > 3) {
        throw new IllegalArgumentException("Was not a date in \"MMM\" format: " + month);
    }
    // Only capitalize the first letter.
    final StringBuilder builder = new StringBuilder();
    builder.append(month.substring(0, 1).toUpperCase())
            .append(month.substring(1).toLowerCase())
            .append("-");
    final StringJoiner stringJoiner = new StringJoiner("-");
    Arrays.stream(components, 1, components.length).forEach(stringJoiner::add);
    builder.append(stringJoiner.toString());
    return builder.toString();
}

【讨论】:

  • 谢谢。我原以为 API 设计人员会分配给这种常见的案例形式变体。谢谢sn-p。
【解决方案4】:

M 根据定义采用July; Jul; 07。您可能想尝试(解决方案有点难看):

date = Character.toUpperCase(date.charAt(0)) + date.substring(1).toLowerCase();
LocalDateTime dt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("MMM-dd-yyyy HH:mm:ss", Locale.US));

或者你可以尝试使用WordUtils from apache-commons for capitalize

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    相关资源
    最近更新 更多