i have a date was hard coded as a string but now i'm to fetch the expired date from the database and compare with it the current time. Here is a sample of the hard coded date stored in the database:2015-02-24T13:00:00.000Z
and now i have to compare this date to the current time and to check if it the date is expired. How can this be resolved by comparing the current date and time in exactly this format?
Asked
Active
Viewed 460 times
0

KENDRA SMITH
- 95
- 1
- 1
- 10
-
1parse, compare, profit. Are you asking how to parse? – Apr 11 '15 at 06:37
-
If so, this is a duplicate of http://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date – Apr 11 '15 at 06:39
1 Answers
0
You can do it in following simple way
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:sss");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = input.parse("2015-02-24T13:00:00.000Z");
System.out.println(d);
String formattedTime = output.format(d);
System.out.println(formattedTime);
The SysOut will be the like this
Tue Feb 24 13:00:00 IST 2015 2015-02-24 13:00:00
For Comparison with current date do it in following way
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:sss");
Date dbDate = input.parse("2015-02-24T13:00:00.000Z");
System.out.println("dbDate"+dbDate);
System.out.println(dbDate.compareTo(new Date()));
sysout will be like this
dbDateTue Feb 24 13:00:00 IST 2015 -1
here output will be -1 means dbDate is older than current date if output is 1 than dbDate is newer than current date.

kavi temre
- 1,321
- 2
- 14
- 21
-
but how could u compare with the current time. Compare current time with the formatted time – KENDRA SMITH Apr 11 '15 at 08:28
-
sorry my computer crashed and just replaced the hard drive. Just tested and it works. Thanks very much sorry for the late reply. – KENDRA SMITH Apr 11 '15 at 23:53