How to get time from SimpleDateFormate() in android?

天大地大妈咪最大 提交于 2019-12-13 03:24:08

问题


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


回答1:


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.




回答2:


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);



回答3:


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")


来源:https://stackoverflow.com/questions/13141511/how-to-get-time-from-simpledateformate-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!