0

I am trying to parse some of the French dates in JAVA. Here are some samples.

  • 25 septembre 2013 a 21h42 par -- Working
  • 1er juillet 2013 a 19h26 par -- Not Working

Here is the code:

Date sampleDate = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRENCH)
            .parse("25 septembre 2013 a 21h42 par");
    System.out.println(sampleDate);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println(simpleDateFormat.format(sampleDate));

I was able to parse successfully (the first date) but not able to parse the second date (1er juillet 2013 a 19h26 par). But If I remove "er" from the date its working. So how can solve this problem.?

Here is the error:

Exception in thread "main" java.text.ParseException: Unparseable date: "1er juillet 2013 a 19h26 par"
at java.text.DateFormat.parse(DateFormat.java:366)
at misc.DateSample.main(DateSample.java:27)

Can any one help me on this.?

Thanks & Regards, Amar.T

Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
Amar
  • 755
  • 2
  • 16
  • 36
  • 1
    I am pretty sure that is not Spanish. Also, I only see the code for one of the dates.. – dquijada Apr 28 '16 at 11:37
  • looks like french to me –  Apr 28 '16 at 11:38
  • 2
    Possible duplicate of [How do you format the day of the month to say "11th", "21st" or "23rd" in Java?](http://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-in-java) Not exactly a perfect duplicate, but the solution do apply –  Apr 28 '16 at 11:39

1 Answers1

2

You can remove the letters in the first token:

String s = "1er juillet 2013 a 19h26 par";
String[] arr = s.split(" ");
arr[0] = arr[0].replaceAll("[\\D]", "");        
s = String.join(" ", arr);

s is now:

"1 juillet 2013 a 19h26 par"

And then use that for your Date.

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93