I am developing a app for sync contacts. Every contact have a update_time field. I want to compare this current update_time to previous update_time.
My problem is I am storing this time as string format in my database.
1.How to convert this string to time? and
2.How to compare the two times?
2014-07-11 15:10:55 this is my time format.
any help or comments welcome. thank you for your valuable answers.
(You may want to consider storing the values in your database as integers instead of as text. There are pros and cons here, so I'll just leave that as a thought which is somewhat separate to your actual question.)
The parsing is reasonably easy, if you're happy to parse into a java.util.Date
:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Specifying the US locale stops anything from being influenced by your device's default locale. (As these are just numbers, it's less likely anyway - but it's just conceivable that it could use a different calendar system.)
As for comparing values:
String.compareTo
; one of the benefits of the ISO-8601 format is that it's sortable. If you're comparing values which are in the database, you can do that in the SQL query in the same way, again because of the way that ISO-8601 is defined.Date
form, and compare those with Date.compareTo
Note that if you can use Joda Time, that's a generally nicer date/time API than java.util.Date
etc.
See more on this question at Stackoverflow