0

I am receiving text files which has dates in different format but when we validate those file we need to convert it to standard format like "yyyy-MM-dd'T'HH:mm:ss".

Is there a common way to recognize which format the date is in txt file and then parse it?

user509755
  • 2,941
  • 10
  • 48
  • 82

4 Answers4

2

No there is no standard way to parse a text and find the original pattern of the dates. Consider the following string

010203

It could be in any the following valid formats (and also others):

ddMMyy
yyMMdd
MMddyy

So for this case is not possible to find the correct format. Generally you need to know the pattern to convert from string to date.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

I think you can use regex to find the date format and then convert it to your standardized format.It is discussed in

Parse any date in Java

Community
  • 1
  • 1
Renganathan V
  • 413
  • 1
  • 9
  • 27
0

you should use a config file, or if every file has a other format you need the configuation on the beginning(Like the ResourcesBundle):

format=yyyy-MM-dd

You should use SimpleDateFormat to parse it.

kpalatzky
  • 1,213
  • 1
  • 11
  • 26
0

Dates are difficult, and as Kayaman wrote, not always possible to parse!

Personally I switched to ISO standard (YYYY-MM-DD) at least 20 years ago, but if you have to process lots of incoming files the best you can do is probably to setup a lot of fuzzy tests to detect the most probable format.

DD/MM/YY vs MM/DD/YY vs YY/MM/DD is just the most common problem, here you have to look at all the dates in a file and see if you can find samples that rule out one or more of the alternatives.

I.e. "if a field is larger than 12 then it can't be months, if larger than 31 then it has to be a year value".

Two-digit year values must be handled in an application-specific manner anyway: Where do you place the pivot value to decide upon the proper century

Bufferering the entire input and trying different alternatives in order to decide upon the most probable won't work for streaming processes, and for some inputs you'll never be absolutely sure.