0

I have several date data in the database with different format like this:

When
---
2021-11-20
2021-11-20 05:03:31
2021-11-20 04:19:25.4
19-11-2021 19:11
19/11/2021 17.39
19 November'21 16:00
19-November-2021
19/11/2021
19-Nov-2021

Now I would like to change them in one go regardless of the formatting it was to dd-mm-yyyy so they can be uniformed in Java. I have found some answers in stack overflow before such as the usage of SimpleDateTime and DateTimeFormatter, but the datetime format were seemed as only one and they wanted to change to one specific datetime form, but in my case, the datetime form seems very random. Any idea would greatly be appreciated. Thanks in advance.

P.S. I would like to sanitize the upcoming inputs from users since it has the input field for "when", but the filling format is still up to user to pick which they like. This is sure not a good practice, but changing this fundamental and most likely used would cause such unnecessary conflict.

Dhana D.
  • 1,670
  • 3
  • 9
  • 33
  • 1
    You can probably attempt parsing formats in sequence and fetch values on a match but unless the incoming formats are well known and non-ambiguous it will always be a fragile process. – Dhrubajyoti Gogoi Dec 03 '21 at 08:51
  • What type do these examples have in your database? Could be an option to normalize them on database/query level. – deHaar Dec 03 '21 at 08:59
  • the formats you specified are quiet varied so don't think there is any out of the box way to parse them. May be you will have to create a small utility where you can keep different patterns in a list and then for each date received from DB, you will have to match it against each defined format. – Sukhmeet Sethi Dec 03 '21 at 09:03
  • Since you mention `SimpleDateTime` and `DateTimeFormatter`: Never use `SimpleDateFormat`. It is a notorious troublemaker of a class and long outdated. Always use `DateTimeFormatter`. In the linked original questions you will find examples of both, so do filter wisely. – Ole V.V. Dec 03 '21 at 09:39
  • The real problem comes when you encounter `02/11/2021`. Then you won’t know whether February 11 or 2 November was intended. – Ole V.V. Dec 03 '21 at 14:57

1 Answers1

0

I would suggest to create a cutom formatter using pattern and use it. Something like:

public static final MY_DATE_TIME_PARSER = new DateTimeFormatterBuilder()
      .appendPattern("yyyy-MM-dd[['T']HH[[':']mm[[':'][ss['.'[SSS][SS][S]]]['Z']]]]").parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
      .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0).parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
      .parseDefaulting(ChronoField.NANO_OF_SECOND, 0).toFormatter();

And then use it as:

String string = "2021-01-01T10:20:30.Z";
LocalDateTime localDateTime = LocalDateTime.parse(string,  MY_DATE_TIME_PARSER);

Try testing it with different inputs and see if it solves your purpose.

vivek
  • 386
  • 3
  • 12
  • I think that it only handles the first of the nine examples in the question. The OP can probably modify it to handle at least a couple more. – Ole V.V. Dec 03 '21 at 10:42
  • 1
    Right. I just suggested an example to create a flexible formatter. – vivek Dec 03 '21 at 12:12