Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java DateFormat: How to deal with "st", "nd", "rd", "th"?

How can I parse a string like this using some DateFormat?

Wednesday, 24th July

As far as I can tell there's nothing in SimpleDateFormat.

like image 573
Ondra Žižka Avatar asked Oct 22 '22 06:10

Ondra Žižka


1 Answers

try this

String str = "Wednesday, 24th July";
str = str.replaceAll("(\\d\\d)..", "$1") + " " + Calendar.getInstance().get(Calendar.YEAR);
Date date = new SimpleDateFormat("EEEE, dd MMMM yyyy", Locale.US).parse(str);
like image 95
Evgeniy Dorofeev Avatar answered Oct 27 '22 16:10

Evgeniy Dorofeev