T
T
Therapyx2016-03-08 12:59:22
Java
Therapyx, 2016-03-08 12:59:22

How to automatically detect date/time format?

The problem is that there are huge text documents that need to be read and calculated time.
If there is always 1 constant format, then after adding a sheet or something like that, it would look like this:

SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
ArrayList<String> obj = new ArrayList<String>();
    obj.add("06.03.2016 09:00:00");
    obj.add("06.03.2016 17:45:00");
    obj.add("06.03.2016 23:51:12");
    obj.add("07.03.2016 00:55:33");

And then you can already do all the necessary calculations. But the problem is that the format may not be 1. And definitely not known / or in the future a new one (so that it does not have to be redone).
And here is the question itself: Are there such methods? I searched on the stack, alas, I did not find anything, there are a lot of questions - there are no solutions.
ps Libraries like pojava unfortunately do not fit, more precisely "maybe", then you need to look at the licenses ... :)
If not, then question number 2)
For example, let's take the format already given dd.MM.yyyy HH:mm:ss . I am writing a function that needs to define manually
String format = "06.03.2016 09:00:00";
format = DetermineFormat(String format){
   if (format.... ????????)
       return dd.MM.yyyy HH:mm:ss
}

how to determine in numerical sequence and send format as "dd.MM.yyyy HH:mm:ss" back?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Therapyx, 2016-03-09
@Therapyx

Since I still didn’t find the answer, I did it all the same with a crutch through checking possible formats

// list of a different format types. If you need new one, just past it here.
  private static List<SimpleDateFormat> dateFormats = new ArrayList<SimpleDateFormat>() {{
      add(new SimpleDateFormat("MM/dd/yyyy hh:mm:ss")); //US
      add(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss")); //EU
      
      add(new SimpleDateFormat("MM.dd.yyyy hh:mm:ss")); //US
      add(new SimpleDateFormat("dd.MM.yyyy HH:mm:ss")); //EU
      
      add(new SimpleDateFormat("MM-dd-yyyy hh:mm:ss")); //US
      add(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss")); //EU
}
};

Reading a string, stuffing it into a function that runs through this sheet and if it finds a suitable format, then returns this string in Date format back ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question