Deepanker Chaudhary
Deepanker Chaudhary

Reputation: 1704

How to get time from SimpleDateFormate() in android?

I have a String this:-

   Tue Oct 30 13:22:58 GMT+05:30 2012;

I want to divide time & date from SimpleDateFormate:-

DateFormat f = new SimpleDateFormat("EEE,MM/dd/yyyy hh:mm:ss);
Date d = f.parse(my string);
DateFormat date = new SimpleDateFormat("MM/dd/yyyy");
DateFormat time = new SimpleDateFormat("hh:mm:ss a");
System.out.println("Date: " + date.format(d));
System.out.println("Time: " + time.format(d));

I am getting this Error:-

 java.text.ParseException: Unparseable date: "Tue Oct 30 13:22:58 GMT+05:30 2012"

Please Tell me whats the problem. Thanks, Deepanker

Upvotes: 0

Views: 309

Answers (3)

dave.c
dave.c

Reputation: 10908

As it stands, your timezone offset does not match the RFC 822 standard, so I don't think you can directly parse the date without performing some cleansing on it first.

If you just want an offset, then the string should look like:

Tue Oct 30 13:22:58 +0530 2012

Note, there is no "GMT", and no colon within the offset. The corresponding pattern is then:

new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy")

Alternatively, you can specify the timezone:

Tue Oct 30 13:22:58 IST 2012

For which the pattern is:

new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy")

Upvotes: 0

Mohammad Ersan
Mohammad Ersan

Reputation: 12444

Are you sure the date is correct?? because I tried to parse it it gives error on GMT+05:30 just remove the :30 and it will work, see here:

String time = "Tue Oct 30 13:22:58 GMT+05 2012";
long f=Date.parse(time);
System.out.println("Time:" + f);

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

Your timestamp string does not match your pattern:

Tue Oct 30 13:22:58 GMT+05:30 2012

is no way like (not to mention syntax error in the SimpleDataFormat initialization line):

EEE,MM/dd/yyyy hh:mm:ss

So you need to make the pattern matching input data. All fields supported by SimpleDateFormat are described here.

Upvotes: 1

Related Questions