java how to parse a string with with spaces and other texts to java.util.date How do you pass "11th Jan, 2012 02:51:01 +0300" String to a java.util.date
Asked
Active
Viewed 436 times
0
-
Search and learn SimpleDateFormat. – Harry Joy Jan 13 '12 at 08:20
-
I'm sorry I don't understand your question. What are you trying to do? – simchona Jan 13 '12 at 08:20
-
SimpleDateFormat cannot parse the day-of-month suffix i.e. the "th" in "11th Jan". – dogbane Jan 13 '12 at 08:24
-
Possible duplicate: http://stackoverflow.com/questions/999172/how-to-parse-date-in-java – Alex K Jan 13 '12 at 14:34
3 Answers
1
Have a look at SimpleDateFormat. You can specify your own format from which a date String
gets parsed to a Date
object.
Try this:
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM, yyyy HH:mm:ss Z");
Date d = sdf.parse("11nd Jan, 2012 02:51:01 +0300".replaceAll("st|nd|rd|th", ""));

Kai
- 38,985
- 14
- 88
- 103
0
Use SimpleDateFormat and create an appropriate format String.
For the ordinals (st/nd/rd/th) see this question: Parsing dates of the format "January 10th, 2010" in Java? (with ordinal indicators, st|nd|rd|th)
-
you can't parse the day suffix i.e. the "th" in "11th Jan" using a SimpleDateFormat. – dogbane Jan 13 '12 at 08:22
-1
You use a SimpleDateFormat to parse your text.
How to convert descriptive dates into Java Date format handles the st/nd/rd/th issue.

Aviram Segal
- 10,962
- 3
- 39
- 52