views:

98

answers:

8

Hi, I am having following function

public static Date parseDate(String date, String format) throws ParseException
 {
         SimpleDateFormat formatter = new SimpleDateFormat(format);
         return formatter.parse(date);
 }

I am using this as follows in my code

Calendar eDate = Calendar.getInstance();
eDate.add(Calendar.DAY_OF_MONTH,10);
Date date = null;
  try {
   date = parseDate(eDate.getTime().toString(),"yyyy-MM-dd hh-mm-ss");
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

But it is throwing -

 java.text.ParseException: Unparseable date

What is the problem here?

+1  A: 

You're currently formatting with the default format from java.util.Date, and then parsing with a potentially different format. You should also change your format string - it's currently using a 12 hour clock with no am/pm indicator, and minutes twice. I think you mean: "yyyy-MM-dd HH-mm-ss"

Jon Skeet
This is the date it is unable to parse "Jul 21, 2010 7:24:07 PM"
psvm
@Vishal: Yes, that's because you're formatting it with java.util.Date.toString.
Jon Skeet
+1  A: 

You're inserting a Zulu Timestamp (UNIX), getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT. Then you define the format as yyyy-mm-dd hh-mm-ss and try to parse the timestamp with this pattern. Which doesn't match.

You could use Date date = calendar.getTime(); and then format it via new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(date);

codedevour
+4  A: 

You'll be happy to hear that there's never a need to parse a date from a Calendar object: The way to pull a Date out of a Calendar is via the getTime() method.


EDIT:

To output the date in eDate in ISO style format:

final DateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String formattedDate = isoFormat.format(eDate.getTime());

That's untested, but I think it should work.

Carl Smotricz
Exactly.... :-)
The Elite Gentleman
Yes but I need it formatted in "yyyy-mm-dd hh-mm-ss"
psvm
You could use `Date date = calendar.getTime();` and then format it via `new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(date);`
codedevour
Then it's time to address the next step. You have a Date, you want a String, at that point you want to use `SimpleDateFormat.format()` on it. I'll put that into my answer...
Carl Smotricz
+1  A: 

Don't use toString() for anything like that. toString() should be used only for debug messages.

Use DateFormat.format(..) to produce a string in a predictable form.

Bozho
A: 

you can simply use the date returned by the calendar, instead of transforming it into string and back into a date (apparently using a wrong date format). The date can be obtained by:

eDate.getTime()

There seems to be no need for SimpleDateFormat in your case.

Eyal Schneider
The `Calendar#getTime()` returns `java.util.Date`, not `long`.
BalusC
@BalusC: thanks, fixed
Eyal Schneider
I was assuming that the sample code was a test case, not actual production code. Had he got the format right, it's actually a pretty good test.
chris
+3  A: 

The format is not stored in the Date. It is stored in the String. The Date#toString() returns a fixed format which is described in its Javadoc.

Do the formatting only at the moment you need to display a Date to a human as a String.

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 10);
Date date = calendar.getTime();
String formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
System.out.println(formattedDate);

Note that MM stands for months and mm for minutes. See also SimpleDateFormat javadoc.

BalusC
Got it running thanks :)
psvm
You're welcome.
BalusC
A: 

Check the Date.toString() method.

The api states that it returns it in the format:

dow mon dd hh:mm:ss zzz yyyy

which is:

Mon Jan 28 14:22:07 EST 2004

You are telling the parser to expect: 2004-01-28 14-22-07

chris
A: 
eDate.getTime().toString()

returns a String representation of a date in this format:

dow mon dd hh:mm:ss zzz yyyy (see the java.util.Date API).

You are trying to parse a date using this format:

yyyy-mm-dd hh-mm-ss .

The code is correctly throwing the ParseException.

dairemac