Skip to content
Advertisement

How to best determine whether the date format is MM/dd or DD/mm in java

I have list of files coming and I need to analyze them using java. The date field is read from files as String and I need to parse it in to LocalDatTime. The problem is it is now known that whether the first part of date is month or day.

The format in them can be anything but most probably in the following formats.

"yyyy-MM-dd HH:mm:ss", "dd/MM/yyyy HH:mm","MM/dd/yyyy HH:mm", "yyyy-MM-dd HH:mm",
        "MM/dd/yy HH:mm"

e.g.

9/8/2020 23:50
9/8/2020 23:55
9/9/2020 00:00

In the above case date field can be guessed from when the date changes from 9/8/2020 23:50 9/9/2020 00:00 . This means the date changed from 8th to 9th and hence the format is MM/dd/yyyy HH:mm

9/8/2020 23:00
9/8/2020 23:50
10/8/2020 00:00

In the above case date field can be guessed from when the date changes from 9/8/2020 23:55 10/9/2020 00:00 . This means the date changed from 9th to 10th and hence the format is dd/MM/yyyy HH:mm

The file can also be 2020-09-08 23:00:00. The only thing I know is that the date will change in the series while the month will seldom change.

What is the best way to cater this issue.

Advertisement

Answer

One of the sulution could be just counting of changes of first and second part and compare result. This is not extra efficient, but quite simple:

// true - day/month/year
// false - month/day
public static boolean isDayMonthYearFormat(List<String> sortedDates) {
    int firstPartChangeAmount = 0;
    int secondPartChangeAmount = 0;
    int prvOne = -1;
    int prvTwo = -1;
    boolean count = false;

    for (String date : sortedDates) {
        String[] parts = date.split("[\/\s+:]");
        int one = Integer.parseInt(parts[0]);
        int two = Integer.parseInt(parts[1]);

        if (count) {
            firstPartChangeAmount += prvOne < one ? 1 : 0;
            secondPartChangeAmount += prvTwo < two ? 1 : 0;
        }

        count = true;
        prvOne = one;
        prvTwo = two;
    }

    if (firstPartChangeAmount == secondPartChangeAmount)
        throw new RuntimeException("can't detect between MM/dd and DD/mm");

    return firstPartChangeAmount > secondPartChangeAmount;
}

Output:

System.out.println(isDayMonthYearFormat(Arrays.asList("9/8/2020 23:50", "9/8/2020 23:55", "9/9/2020 00:00")));  // false
System.out.println(isDayMonthYearFormat(Arrays.asList("9/8/2020 23:00", "9/8/2020 23:50", "10/8/2020 00:00"))); // true
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement