2

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 time and date?

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

1 Comment

In my case, I had to put hyphens, a space, and colons in the parameter: ... = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.