【问题标题】:Apache date utils parseDateStrictly method dosen't work properlyApache dateutils parseDate Strictly 方法无法正常工作
【发布时间】:2020-10-30 05:17:45
【问题描述】:
DateUtils.parseDateStrictly("28 Sep 2018" , "dd MMMM yyyy")

上述日期的格式应该是 dd MMM yyyy (MMM 表示更短的月份),但是 MMMM 也会解析更短的月份,导致解析无效。我已经在使用 parseDateStrictly 方法。还有其他建议吗?

【问题讨论】:

  • MMMM 对您的数据无效时,为什么要使用它?显然日期字符串数据是动态的而不是格式的,对吧?
  • 用例是我有一个主要格式为 dd MMMM yyyy 的日期列,当我在列中遇到此类日期时,我想抛出错误
  • 这意味着dd MMM yyyy 是正确的格式,您想为28 Sept 2018 等其他情况抛出错误,对吧?
  • 列格式为 dd MMMM yyyy,它应该解析像 2018 年 9 月 28 日这样的日期,并且应该在 2018 年 9 月 28 日这样的值上抛出错误

标签: java date parsing apache-commons-dateutils


【解决方案1】:

java.time

我建议您将 java.time 用于您的日期和时间工作。一旦我们只有设计不佳的DateSimpleDateFormat 类可以使用,Apache DateUtils 就很有用。我们不再需要它了。很长一段时间我们都不需要它。

java.time 的行为方式与您期望的开箱即用一样。

    DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofPattern("dd MMMM uuuu", Locale.ENGLISH);
    String dateString = "28 Sep 2018";
    LocalDate.parse(dateString, dateFormatter);

结果:

Exception in thread "main" java.time.format.DateTimeParseException: Text '28 Sep 2018' could not be parsed at index 3
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
    at java.base/java.time.LocalDate.parse(LocalDate.java:428)
    (etc.)

链接

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

【讨论】:

    【解决方案2】:

    列格式为 dd MMMM yyyy,它应该解析像 28 这样的日期 2018 年 9 月,应该在 2018 年 9 月 28 日等值上抛出错误

    DateUtils 使用 java.util 的日期时间 API 及其格式 API SimpleDateFormat,它们已过时且容易出错。我建议你应该完全停止使用它们并切换到modern date-time API

    使用现代日期时间 API:

    import java.time.DateTimeException;
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Locale;
    
    public class Main {
        public static void main(String[] args) {
            // Test strings
            String[] arr = { "28 September 2018", "28 Sep 2018", "28 09 2018" };
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMMM uuuu", Locale.ENGLISH);
            for (String s : arr) {
                try {
                    LocalDate date = LocalDate.parse(s, formatter);
                    // ...Process date e.g.
                    System.out.println(DateTimeFormatter.ofPattern("MMMM dd, uuuu", Locale.ENGLISH).format(date));
                } catch (DateTimeException e) {
                    System.out.println(s + " is not a valid string.");
                }
            }
        }
    }
    

    输出:

    September 28, 2018
    28 Sep 2018 is not a valid string.
    28 09 2018 is not a valid string.
    

    通过 Trail: Date Time 了解有关现代日期时间 API 的更多信息。

    如果您正在为您的 Android 项目执行此操作,并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

    【讨论】:

      猜你喜欢
      • 2016-12-01
      • 2017-08-15
      • 2014-02-23
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      相关资源
      最近更新 更多