0

Possible Duplicate:
How to convert a date String to a Date or Calendar object?

How can I convert "16 Nov 2011 08:00" to a Date object? I tried SimpleDateFormat but does not works, I get

java.text.ParseException: Unparseable date: "16 Nov 2011 08:00"

or will I have to split the string enter slashes and then give it a try ?

Community
  • 1
  • 1
simranNarula
  • 641
  • 3
  • 9
  • 14
  • What date is that supposed to represent? A `Date` represents a single point in time, not a period. Also, with `SimpleDateFormat` you give it a custom format. It would obviously be relevant what format you're giving it. – Mark Peters Nov 08 '11 at 05:12
  • There's a pretty significant difference between your question's **title** and its **body**. – Michael Petrotta Nov 08 '11 at 05:15
  • Sorry guys, I surely know that I cannot convert time range in a Date object, there was mistake in the question I have updated it, thanks for Input – simranNarula Nov 08 '11 at 05:16

2 Answers2

3

You problem with the SimpleDateFormat was probably an incorrect format string.

Here is the correct way:

DateFormat formatter = new SimpleDateFormat( "dd MMM yyyy HH:mm" );
Date date = formatter.parse( "16 Nov 2011 08:00" );
Strelok
  • 50,229
  • 9
  • 102
  • 115
1

You will need to use SimpleDateFormat to set a date format then use .parse(String s) to convert your String to a Date, here's an example:

    DateFormat format = new SimpleDateFormat("dd MMM yyyy HH:mm");
    String theDateString = "16 Nov 2011 08:00";
    Date myDate = format.parse(theDateString);
Deco
  • 3,261
  • 17
  • 25