开发者

How can I read and parse a date and time in Android?

开发者 https://www.devze.com 2023-02-20 17:02 出处:网络
I have a date and time 2011-02-28 07:00:52 as a string. How can I read it and then parse it as a date and time to compare it to an Android system tim开发者_JS百科e and date?String time; // your string

I have a date and time 2011-02-28 07:00:52 as a string. How can I read it and then parse it as a date and time to compare it to an Android system tim开发者_JS百科e and date?


String time; // your string representing the time
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMddHHmmss");
Date startDate = dateFormatter.parse(time);

and

new Date() 

will give you a date representing the current date and time.


When comparing dates, once you have the date object as shown by kett_chup, use date.getTime() which gives you the milliseconds and you can then easily compare with another date.getTime() to determine if one date came before or after the other.

0

精彩评论

暂无评论...
验证码 换一张
取 消