1

Hey Im using the code below to parse dates in different formats.

The trubel is, it takes the first format and parses the date even if it doesn't fit.

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;
}

Console:

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

OR

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

So I don't get why does the first format allways parse?

Zero Soul Eater
  • 132
  • 1
  • 13

2 Answers2

2

your loop stops at the very first returnstatement, remove the return statement from your loop, then it will lopp all over your date formats.

EDIT : as requested in the comments:

  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;
}

Here above I considered you only have two possible formats.

Mustapha Belmokhtar
  • 1,231
  • 8
  • 21
2

Here is a simpler example in jshell. the "d-M-y" does parse both because it defaults to lenient. If you do setLenient(false) it will exception on the date in the other format.

|  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)

FYI, the Java 8+ API way to do this:

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)
clay
  • 18,138
  • 28
  • 107
  • 192