1

I have a file in which nth column has a date. say like below(Each column is divided by ',')

1234,3-23,34,kjiio,20131015,AS

While I am parsong this data assuming that date is 'YYYYMMDD', I am able to get good results. But my problem is the date is not unique, it has below formats,

2013-10-15
10152013
2013-10-15T00:00:00.000

so my requirement is to make all the records in that file to be in single format or is there any java api that takes a date of any format as aboce and gives in a format that I requested.

Thanks!

Mahesh
  • 99
  • 1
  • 5
  • 1
    You want to look at questions like: http://stackoverflow.com/questions/11446420/parse-string-to-date-java?rq=1 – reto Nov 19 '13 at 11:44

2 Answers2

1

If you can identify the timestamp by its position, and if you know all formats, you can check which format applies:

String dateString = "...";
Date date;
if (dateString.contains("-")) {
    if (dateString.contains(":")) {
        // Format 1: YYYY-MM-DDTHH-mm.sss
        date = parseFormatOne(dateString);
    } else {
        // Format 2: YYYY-MM-DD
       date = parseFormatTwo(dateString);
    }
} else {
    // Format 3: DDMMYYYY
    date = parseFormatThree(dateString);
}

Then you can a) replace the value in the file with that one formatted according to your definition or b) simply handle the parsed Date.

Robin Krahl
  • 5,268
  • 19
  • 32
0

If you know which column it is, you can split your string using delimiters and get the nth entry from the resulting arraylist, else you'll have to use RegEx to find it.

As for the homogenizing: If there is only a limited amount of ways they write it (for example if those are the only 3 ways), you might just want to parse them to a date yourself.

Voidpaw
  • 910
  • 1
  • 5
  • 18
  • I know its column, It is always 10th column, to make the records in a single date format, you mean to write a java program to read that file ,get the records one by one, get the 10th column,format it using DATE API and write back to another file? – Mahesh Nov 19 '13 at 12:05
  • The code I was about to write is very similar to Robin Krahl's code (down below?). But basically: yes - you want to do it just about as you described – Voidpaw Nov 19 '13 at 12:17