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.

JavaScript

e.g.

JavaScript

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

JavaScript

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:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement