开发者

How do I compare a raw time in Java to now? [duplicate]

This question alre开发者_StackOverflow社区ady has answers here: Closed 11 years ago.

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);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜