How do I compare a raw time in Java to now? [duplicate]
Possible Duplicate:
How do I compare a raw time in Java?
For example suppose I have
String endTime = "16:30:45";
How would I determine whether right now is before this time? I have tried
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss")
Date date = sdf.parse(endTime);
However date has year 1970 and so the comparisons fail
You can always do the inverse:
"16:30:45".compareTo(new SimpleDateFormat("HH:mm:ss").format(new Date()))
//Parse the time from String using SimpleDateFormat and set it to cal, reset other fields
Calendar cal = Calendar.getInstance();
cal.setTime(parsedTimeinDate);
cal.set(Calendar.MONTH,0);
cal.set(Calendar.YEAR,2000);
cal.set(Calendar.DATE,1);
//reset other fields for now calendar those aren't related to time
Calendar calNow = Calendar.getInstance();
calNow .set(Calendar.MONTH,0);
calNow .set(Calendar.YEAR,2000);
calNow .set(Calendar.DATE,1);
//determine
cal.before(calNow);
精彩评论