1

I have two dates as following that need to format them but I am receiving following exception. My main issue is with the th rd etc in the day part. I could not find any answer for this question. I checked all these links 1,2,3,4,5 I think I should use Regex but not sure how.

 10th Dec 2019 -> 2019-12-10 
 10th December 2019 -> 2019-12-10

Code

 String date1 = "10th Dec 2019";
 Date date = new SimpleDateFormat("dd MMMM YYYY").parse(date1);
 System.err.println(date);
 String date2 = new SimpleDateFormat("yyyy-mm-dd").format(date);
 System.err.println(date2);

Exception

 Exception in thread "main" java.text.ParseException: Unparseable date: "10th Dec 2019"
Community
  • 1
  • 1
Jack
  • 6,430
  • 27
  • 80
  • 151

6 Answers6

5

Replace all the expected undesired sufiixes by an empty string, and then parse:

    String s = "10th Dec 2019";

    SimpleDateFormat fmt = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
    Date d = fmt.parse(s.replaceFirst("th|nd|st|rd", ""));
    System.out.println("d = " + d);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

You could remove the quantifier first, then parse the date as usual.

Something like this would work:

String[] input = {     "10th Dec 2019",
                       "10th December 2019",
                       "1st December 2019",
                       "3rd December 2019"
                 };

DateFormat df = new SimpleDateFormat("dd MMMM yyyy");
DateFormat result = new SimpleDateFormat("dd-MM-yyyy");
for(String s : input) {
    s = s.replaceAll("^(\\d+).{2}", "$1");  //The .{2} will match any two characters, which should be the th, st, nd and rd.
    System.out.println(result.format(df.parse(s)));
}

Yields:

10-12-2019
10-12-2019
01-12-2019
03-12-2019
npinti
  • 51,780
  • 5
  • 72
  • 96
0

You can escape explicit literals in date formats by putting them in single quotes within the pattern like that:

Date date = new SimpleDateFormat("dd'th' MMMM YYYY").parse(date1);.

However, that solution does not support wildcards, which I assume you'd need for "st" and "nd" suffixes. So I suggest you do some preprocessing of the input based on a regex to drop the "st/nd/th" suffix.

Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
0

For your String you can use:

Date date = new SimpleDateFormat("dd'th' MMMM YYYY").parse(date1);
Jens
  • 67,715
  • 15
  • 98
  • 113
0

In Java 7, it's concidered best practice to use Joda time for handling dates. So I recommend you look into it. The api is way easier to use then Date. (Parsing too)

When you later migrate to Java 8, the java.time (JSR-310) package is based on Joda Time, so you will have an easy time migrating.

tomaj
  • 1,570
  • 1
  • 18
  • 32
  • Actually, java.time is inspired by Joda-Time but not based on it. Concepts are largely the same but your code may differ significantly. Best to start with java.time if on Java 8 or later. – Basil Bourque Sep 01 '15 at 15:17
  • Can you expand your answer to show some example code to more directly address the Question? – Basil Bourque Sep 01 '15 at 15:20
-1

You can try this way:

String suffix = getSuffix(inputDate.get(Calendar.DAY_OF_MONTH));
DateFormat dateFormat = new SimpleDateFormat(" d'" + suffix + "' MMMM yyyy");
return dateFormat.format(currentCalDate.getTime());

// getSuffix
private String getSuffix(int inputDay) {
    if (day >= 11 && day <= 13) {
        return "th";
    }
    switch (day % 10) {
        case 1:
            return "st";
        case 2:
            return "nd";
        case 3:
            return "rd";
        default:
            return "th";
    }
}
Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
  • The suffix can just be replaced instead of adding the suffix in the DateFormat. That's easier and cleaner for this problem. – Karthik R Sep 01 '15 at 10:27