如何比较Java中的原始时间? [英] How do I compare a raw time in Java?
本文介绍了如何比较Java中的原始时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如假设我有
String endTime = "16:30:45";
我如何确定现在是否在此之前?
How would I determine whether right now is before this time?
推荐答案
首先,你需要解析这个:
First, you need to parse this:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss")
Date date = sdf.parse(endTime);
然后你可以创建一个日历来比较时间:
Then you can create use a calendar to compare the time:
Calendar c = Calendar.getInstance();
c.setTime(date);
Calendar now = Calendar.getInstance();
if (now.get(Calendar.HOUR_OF_DAY) > c.get(Calendar.HOUR_OF_DAY) .. etc) { .. }
或者,您可以创建一个日历,如 now
,并将其HOUR,MINUTE和SECOND字段设置为新日历中的字段。
Alternatively, you can create a calendar like now
and set its HOUR, MINUTE and SECOND fields with the ones from the new calendar.
使用joda-time,你可以做类似的事情。
With joda-time you can do something similar.
new DateMidnight().withHour(..).widthMinute(..).isBefore(new DateTime())
这篇关于如何比较Java中的原始时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文