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 ?

3
  • 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. Commented Dec 4, 2015 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. Commented Dec 4, 2015 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. Commented May 20, 2020 at 6:51

5 Answers 5

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);
    }

}
Sign up to request clarification or add additional context in comments.

5 Comments

PrettyTimeParser .. a lot of libraries appear for it , which one plz ?
I've edited the answer, click on natty. and go to github page and download the lastest version
You can find jar file this: mvnrepository.com/artifact/com.joestelmach/natty/0.12 , but using 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
@ViacheslavVedenin , here whuch library: List groups = parser.parse("the day before next thursday"); ?
If you know list of possible data patterns, much better use list of patterns as I show her stackoverflow.com/a/34090142/4318868 . You realy need parse "the day before next thursday" in your application ?
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).

Comments

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);   
    }

1 Comment

you specified the pattern "dd/MM/yyyy" ! , i want to parse without knowing that is the pattern
1

Look at the Cacovsky's answer to this question.

Comments

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)

1 Comment

While this code may solve the question, including an explanation 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.