0

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".

Gunwant
  • 949
  • 1
  • 14
  • 29
  • 5
    Impossible. How do you handle the date format I just invented? Days are hexadecimal, months are written in Portugese, years are binary. Today is the `1D-janeiro-11111100100`. Obvious hyperbole, but you get my point. Accepting "any" date format is not an acceptable requirement, because "any" means ***any***. – Michael Jan 29 '20 at 13:14
  • 2
    "*the date format received from these agents varies*" Your problem here is that your interfaces are not well-defined. You try to tolerate garbage, so they provide garbage. Define your interface more strictly and give them a format that they must adhere to. – Michael Jan 29 '20 at 13:18
  • 1
    "*I have to change the code handle that format*" Keep a list of allowed formats in a config file. Have your application read the formats from the config file. – Michael Jan 29 '20 at 13:20
  • 1
    I read the question as the "agents" are someone else's doing and the data is already in the database as TIMESTAMP(6) which should not need any parsing, what are you using to pull the data from the TIMESTAMP(6) columns? – kendavidson Jan 29 '20 at 13:26
  • Any reason why the agent that provides the dates doesn't also provide the pattern? That way your program could handle "any" pattern. – Scratte Jan 29 '20 at 14:04
  • This has been asked with variations before. The general answer is that it’s impossible, but some creative inventions have also been posted. Search and see if you can use any of them. – Ole V.V. Jan 30 '20 at 14:42

0 Answers0