I have a specific requirement, a third party application provides data to my application. The data is inserted in to various (depending up on the data agent) Oracle database tables. This data contains one or more columns having data type as "timestamp(6) with time zone". My application consumes data from these tables.
My problem is, the date format received from these agents varies (also depends on agents which are inserting data in to table.) I have to handle all such formats, and if some new format encountered tomorrow, my application throws exception and skips that record. I am handling this situation with below example code by using "joda-time" library
For example -
DateTimeParser[] parsers1 = { DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss Z").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss a Z").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS Z").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSSSSS Z").getParser(),
DateTimeFormat.forPattern("yyyy.MM.dd HH:mm:ss Z").getParser(),
DateTimeFormat.forPattern("yyyy.MM.dd HH:mm:ss a Z").getParser(),
DateTimeFormat.forPattern("yyyy.MM.dd HH:mm:ss.SSS Z").getParser(),
DateTimeFormat.forPattern("yyyy.MM.dd HH:mm:ss.SSSSSS Z").getParser(),
DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss Z").getParser(),
DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss a Z").getParser(),
DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss.SSS Z").getParser(),
DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss.SSSSSS Z").getParser()
}; // Here i have to take care of too many date formats
DateTimeFormatter dateStringFormat = new DateTimeFormatterBuilder().append(null, parsers1).toFormatter()
.withOffsetParsed();
// This code throws exception
DateTime date = DateTime.parse(dateString, dateStringFormat);
// This should be my final date format
String profileImportDate = date.toString("yyyy-MM-dd'T'HH:mm:ss ZZ");
The array DateTimeParser keeps all the formats that my application suppose to receive, but lets say tomorrow a new format require, I have to change the code handle that format.
I do not want to handle such situation case by case, but some uniform way, by which I can convert date with any pattern to my pattern which is "yyyy-MM-dd'T'HH:mm:ss ZZ".