Amala
Amala

Reputation: 161

Convert String to Date Time Format

I have to compare 2 dates which are in String format as: Fri Aug 23 17:03:19 IST 2013

I am trying to use new SimpleDateFormat("dd-MM-yyyy HH-mm-ss") to convert in DateTime so that dates can be comparable .

tempTimeStamp=Fri Aug 23 17:03:19 IST 2013
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss"); 
Date startDate;

startDate = df.parse(tempTimestamp);
String newDateString = df.format(startDate);

But its showing error object of this type can't be converted in DateFormat.Please help..

Upvotes: 1

Views: 7959

Answers (3)

Ivan Foong
Ivan Foong

Reputation: 219

You were using the wrong format to try and parse the datetime string, try using the following snippet:

String tempTimeStamp="Fri Aug 23 17:03:19 IST 2013"
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date startDate = startDate = df.parse(tempTimestamp);
String newDateString = df.format(startDate);

You can look up more on the patterns for defining the date time format at http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Upvotes: 2

Scary Wombat
Scary Wombat

Reputation: 44854

The format of your SimpleDateFormat does not match that of the String that you are using.

The input String should match dd-MM-yyyy HH-mm-ss

If you are wanting simply to format the output of a Date, then no parsing of a String is necessary.

Upvotes: 0

Crickcoder
Crickcoder

Reputation: 2155

Your expected format doesn't matches with the one provided.

Upvotes: 0

Related Questions