【问题标题】:Java date with different formats [duplicate]不同格式的Java日期[重复]
【发布时间】:2017-11-25 15:41:01
【问题描述】:

嘿,我使用下面的代码来解析不同格式的日期。

trubel 是,它采用第一种格式并解析日期,即使它不适合。

public static Date parseDate(String date) {
    date = date.replaceAll("[-,.;:_/&()+*# ']", "-");
    System.out.print(date);
    List<String> formatStrings = Arrays.asList("d-M-y","y-M-d");
    for (String formatString : formatStrings) {
        try {
            System.out.println(new SimpleDateFormat(formatString).parse(date));
            return new SimpleDateFormat(formatString).parse(date);
        } catch (Exception e) {
        }
    }
    return null;
}

控制台:

1994-02-13
Wed Jul 18 00:00:00 CEST 2018

13-02-1994
Sun Feb 13 00:00:00 CET 1994

所以我不明白为什么第一种格式总是解析?

【问题讨论】:

  • 因为您正在解析字符串。不格式化

标签: java date


【解决方案1】:

这里有一个更简单的 jshell 示例。 “d-M-y”确实会解析两者,因为它默认为宽松。如果您执行setLenient(false),它将在其他格式的日期上出现异常。

|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

jshell> java.text.SimpleDateFormat dmy = new java.text.SimpleDateFormat("d-M-y");
dmy ==> java.text.SimpleDateFormat@596ca10

jshell> dmy.parse("13-02-1994")
$2 ==> Sun Feb 13 00:00:00 CST 1994

jshell> dmy.parse("1994-02-13")
$3 ==> Wed Jul 18 00:00:00 CDT 2018

jshell> dmy.isLenient()
$4 ==> true

jshell> dmy.setLenient(false)

jshell> dmy.parse("1994-02-13")
|  java.text.ParseException thrown: Unparseable date: "1994-02-13"
|        at DateFormat.parse (DateFormat.java:388)
|        at (#6:1)

仅供参考,Java 8+ API 的实现方式:

jshell> java.time.format.DateTimeFormatter dmy = java.time.format.DateTimeFormatter.ofPattern("dd-MM-yyyy");
dmy ==> Value(DayOfMonth,2)'-'Value(MonthOfYear,2)'-'Value(YearOfEra,4,19,EXCEEDS_PAD)

jshell> java.time.LocalDate.parse("13-02-1994", dmy)
$2 ==> 1994-02-13

jshell> java.time.LocalDate.parse("1994-02-13", dmy)
|  java.time.format.DateTimeParseException thrown: Text '1994-02-13' could not be parsed at index 2
|        at DateTimeFormatter.parseResolved0 (DateTimeFormatter.java:1988)
|        at DateTimeFormatter.parse (DateTimeFormatter.java:1890)
|        at LocalDate.parse (LocalDate.java:428)
|        at (#3:1)

【讨论】:

  • 谢谢你,这是一个很好的答案:)
  • 我当然推荐 Java 8 方式。感谢您提供这个。
【解决方案2】:

您的循环在第一个 returnstatement 处停止,从循环中删除 return 语句,然后它将覆盖您的所有日期格式。

编辑:根据 cmets 的要求:

  public static Date parseDate(String date) {
    Date dateToReturn = null;
    date = date.replaceAll("[,\\.;:_/&\\(\\)\\+\\*# ']", "-");
    String formatString = "d-M-y";
    if (date.matches("\\d{4}-\\d{2}-\\d{2}")) {
        formatString = "y-M-d";
    }
    try {
        dateToReturn = new SimpleDateFormat(formatString).parse(date);
    } catch (Exception e) {
        System.out.println("the given date does not math this format " + formatString);
    }
    return dateToReturn;
}

上面我认为你只有两种可能的格式。

【讨论】:

  • 我想返回一个Date,但是如果有异常应该返回?
  • 好的,但基于哪种格式? ,因为有两种格式让我很困惑
  • 好吧,我在网络后端使用它,我可以从浏览器中获得不同的格式
  • 我编辑了答案,希望对您有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 2021-04-07
  • 2014-01-12
相关资源
最近更新 更多