0

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

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130

3 Answers3

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)

Community
  • 1
  • 1
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
-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