-6

if i try replace("st", "") so it occurs URLjava.text.ParseException: Unparseable date: "Augu 16, 1979"

please help ....

DateFormat originalFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
    DateFormat targetFormat = new SimpleDateFormat("yyyyMMdd");
    Date date = originalFormat.parse("August 21st, 2012");
    String formattedDate = targetFormat.format(date);  
Panu Haaramo
  • 2,932
  • 19
  • 41
  • 3
    Hi Chaitanya. Try to rewrite question to better fit our community standards please. https://stackoverflow.com/help/how-to-ask – amer Dec 31 '19 at 08:36
  • Does this answer your question? [Parsing a date’s ordinal indicator ( st, nd, rd, th ) in a date-time string](https://stackoverflow.com/questions/28514346/parsing-a-date-s-ordinal-indicator-st-nd-rd-th-in-a-date-time-string) – snieguu Dec 31 '19 at 10:52
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 01 '20 at 21:27
  • I am immodest enough to recommend my own answer to the linked original question [here](https://stackoverflow.com/a/44198044/5772882) or [here](https://stackoverflow.com/a/57116982/5772882) or my answer to a related question [here](https://stackoverflow.com/a/50369812/5772882). – Ole V.V. Jan 02 '20 at 04:43

3 Answers3

1

The problem is that replace("st", "") also removes the st ending of August, resulting in the input string seen in the error message.

To handle this, you need to make sure the st suffix is right after a digit, so it's part of the day value. You also need to handle all of 1st, 2nd, 3rd, and 4th.

This means that you should use regular expression, like this:

replaceFirst("(?<=\\d)(?:st|nd|rd|th)", "")

Test

public static void main(String[] args) throws Exception {
    test("August 20th, 2012");
    test("August 21st, 2012");
    test("August 22nd, 2012");
    test("August 23rd, 2012");
    test("August 24th, 2012");
}
static void test(String input) throws ParseException {
    String modified = input.replaceFirst("(?<=\\d)(?:st|nd|rd|th)", "");

    DateFormat originalFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
    DateFormat targetFormat = new SimpleDateFormat("yyyyMMdd");
    Date date = originalFormat.parse(modified);
    System.out.println(targetFormat.format(date));
}

Output

20120820
20120821
20120822
20120823
20120824
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • For my part I would not do any replacement in the original string before parsing it. No matter if we’re talking the modern `DateTimeFormatter` or the notorious and outdated `SimpleDateFormat`, both are capable of parsing the string as it is using optional parts and literal texts as shown in some of the answers to the linked original questions. – Ole V.V. Jan 02 '20 at 04:46
0

MMMM format should give full month name. Try to use Locale.US .

amer
  • 1,528
  • 1
  • 14
  • 22
  • 1
    This seems to be beside the problem that the questioner is having (see the other answers for details; I admit it’s not very well explained in the question itself). – Ole V.V. Jan 01 '20 at 21:29
0

You are replacing "st" also in "August". Use replace("1st", "1").

Panu Haaramo
  • 2,932
  • 19
  • 41
  • It sounds simple, but will grow as you need to do it for 1st, 2nd, 3rd, 4th, …, 11th, 12th, … up to 31st. – Ole V.V. Jan 02 '20 at 18:46