2

If i have a string that contain a date in a format (unknown) "d/MM/YY" or "m:d:YYYY" etc.).

How could i parse it and get day,month,year values ?

I tried to parse them by filling an array with all format combinations , and try to parse the date with each one of them , i think it's a stupid solution !

Any ideas ?

Minions
  • 5,104
  • 5
  • 50
  • 91
  • 2
    What does the "etc" mean? If you can accept "dd/MM/yyyy" and "MM/dd/yyyy" (both of which are pretty common) then it's game over - you can't hope to tell whether "04/12/2015" is December 4th or April 12th. – Jon Skeet Dec 04 '15 at 13:58
  • If you have a lot of string with one pattern - game isn't over, you can analyze all string and found where day, and where month statistically. – Slava Vedenin Dec 04 '15 at 14:14
  • It's simple enough to work with strings but if you don't know the format then its impossible to sensibly decode day, month and year, how would you know the order of the fields? You could just extract all the fields on a non-numeric delimiter. – SPlatten May 20 '20 at 06:51

5 Answers5

3

your best bet is to use natty

very useful library,

here is an example of how to use it:

public static Date parse(String date) throws Exception {

    try{

        List<DateGroup> parse = new PrettyTimeParser().parseSyntax(date);
        return parse.get(0).getDates().get(0);

    }catch (Exception e){
        throw new Exception("unparseable date: " + date);
    }

}
nafas
  • 5,283
  • 3
  • 29
  • 57
  • PrettyTimeParser .. a lot of libraries appear for it , which one plz ? – Minions Dec 04 '15 at 14:15
  • I've edited the answer, click on natty. and go to github page and download the lastest version – nafas Dec 04 '15 at 14:16
  • 1
    You can find jar file this: http://mvnrepository.com/artifact/com.joestelmach/natty/0.12 , but using http://natty.joestelmach.com/try.jsp I have stange results: 01.01.15 = Fri Dec 04 01:01:15 UTC 2015, 21/12/2015 = Fri Dec 04 21:00:00 UTC 2015, 01:11:2015 = Fri Dec 04 01:11:20 UTC 2015 and so on. So it's look like not very reliable soulition – Slava Vedenin Dec 04 '15 at 14:30
  • @ViacheslavVedenin , here whuch library: List groups = parser.parse("the day before next thursday"); ? – Minions Dec 04 '15 at 14:48
  • If you know list of possible data patterns, much better use list of patterns as I show her http://stackoverflow.com/a/34090142/4318868 . You realy need parse "the day before next thursday" in your application ? – Slava Vedenin Dec 04 '15 at 14:56
1

Try to use this:

  public static void main(String []args){
        SimpleDateFormat dt = new SimpleDateFormat(); 
        TimeZone date;
        date = dt.getTimeZone();
  Calendar cal = Calendar.getInstance().getInstance(Locale.UK);
  cal.setTimeZone(date);
  int year = cal.get(Calendar.YEAR);
  int month = cal.get(Calendar.MONTH);
  int day = cal.get(Calendar.DAY_OF_MONTH);

        System.out.println(year);  
        System.out.println(month); 
        System.out.println(day);   
    }
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
1

If you unknown pattern format you can use something like this

    DateTimeFormatter formatter1 = DateTimeFormat.forPattern("d/MM/YY")
            .withLocale(Locale.UK);
    DateTimeFormatter formatter2 = DateTimeFormat.forPattern("m:d:YYYY")
            .withLocale(Locale.UK);
    ...
    DateTimeFormatter formatterN = DateTimeFormat.forPattern(...

    String stringDate = "08:18:2012";
    LocalDate date;
    try {
        date = formatter1.parseLocalDate(stringDate);
    } catch (Exception exp) {
       try {
          date = formatter2.parseLocalDate(stringDate);
       } catch (Exception exp) {
          ...
          date = formatterN.parseLocalDate(stringDate);
       }
    } 

OR using List:

    List<DateTimeFormatter> formatterList = new ArrayList<>();
    formatterList.add(DateTimeFormat.forPattern("d/MM/YY")
            .withLocale(Locale.UK));
    formatterList.add(DateTimeFormat.forPattern("m:d:YYYY")
            .withLocale(Locale.UK));
    ...
    formatterList.add(DateTimeFormat.forPattern(...

    String stringDate = "08:18:2012";
    LocalDate date;
    for(DateTimeFormatter formatter : formatterList) {
       try {
          return formatter.parseLocalDate(stringDate);
       } catch (Exception exp) {

       }
    } 

But it's impossible if you have pattern like "d/MM/YY" and "MM/d/YY", because you can recognize what string "01/01/15" means (where day and where month). Only if you have a lot of strings with one pattern you can statistically undestand what is day and what is month (month never be more then 12).

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
1

Look at the Cacovsky's answer to this question.

Benny Bottema
  • 11,111
  • 10
  • 71
  • 96
Pavoletto
  • 140
  • 10
0
//download library:   org.ocpsoft.prettytime.nlp.PrettyTimeParser

String str = "2020.03.03";
Date date = new PrettyTimeParser().parseSyntax(str).get(0).getDates().get(0);
System.out.println(date)
double-beep
  • 5,031
  • 17
  • 33
  • 41
Mahdi
  • 57
  • 6
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. [From Review](/review/low-quality-posts/26177359) – double-beep May 20 '20 at 11:05