0

Is there any class in Java that could convert incoming dates , which would be in different formats into a standard format .I know SimpleDateFormat does convert date to specific formats, but I need a method that could actually convert all below dates in format which is YYYY-MM-DD_HH:MM:SS_inputstring:

inputs

20170112_125645
20170915 137546
09122017:135292
2014012014132390 -- milliseconds in here

output

YYYY-MM-DD_HH:MM:SS_inputstring

We are expecting input date values in multiple formats, which should be converted to YYYY-MM-DD_HH:MM:SS_inputstring I know this is a very common question , but I couldn't find one specific to my scenario .

Neethu Lalitha
  • 3,031
  • 4
  • 35
  • 60
  • Related: [Parse any date in Java](https://stackoverflow.com/questions/3389348/parse-any-date-in-java) – Pshemo Apr 26 '17 at 20:33
  • So you want some magic class??? I don't see how `2014012014132390` can be automagically™ parsed –  Apr 26 '17 at 20:33
  • *Any* format? No. But there are several utilities available that'll parse from a collection of formats. – Dave Newton Apr 26 '17 at 20:34
  • *"Is there any class in Java that could ..."* Yes, there is, when you write it. – Andreas Apr 26 '17 at 20:35
  • 1
    What time of day is `787546` supposed to be? --- Differentiating `ddMMyyyy` and `MMddyyyy`, solely by looking at the value, is impossible, so you can never handle *any* format. – Andreas Apr 26 '17 at 20:38

2 Answers2

2

If you know all the possible incoming date formats, you could create a SimpleDateFormat instance for each of them. Then for every incoming date string you just run it through the list. If a format won't parse this string it will throw an exception, catch it and move on to the next format, rinse and repeat until you get to one that parses the string.

jingx
  • 3,698
  • 3
  • 24
  • 40
  • http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat This also helped – Neethu Lalitha Apr 26 '17 at 20:53
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 26 '17 at 21:06
1

Using java.time

No magic way to decipher any random format. But in your case you have easy ways to identify each of the particular formats.

DateTimeFormatter f ;
if ( input.contains( "_" ) ) {  // 20170112_125645
    f = DateTimeFormatter.ofPattern( "uuuuMMdd'_'HH:mm:ss" );

} else if ( input.contains( " " ) ) {  // 20170915 137546
    f = DateTimeFormatter.ofPattern( "uuuuMMdd' 'HH:mm:ss" );

} else if ( input.contains( ":" ) ) {  // 09122017:135292
    f = DateTimeFormatter.ofPattern( "ddMMuuuu':'HH:mm:ss" );

} else if ( input.length() == 16 ) {  // 2014012014132390
    f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmssSS" );

} else {
    … // Handle error condition
    System.out.println( "ERROR - Unexpected input: " + input ) ;

}

LocalDateTime ldt = LocalDateTime.parse( input , f );

Of course in real code being called many times often, I would cache those DateTimeFormatter instances rather than instantiate each time. Perhaps define an Enum if used in various other places in your code base.

Usually I recommend always specifying a Locale in the formatter rather than rely implicitly on JVM’s current default. But here I don't think the locale has any effects.

Avoid legacy date-time classes

You mentioned SimpleDateFormat. That class is one of the troublesome old date-time classes that should be avoided. Now legacy, supplanted by the java.time classes.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154