开发者

Java Date & Time of Day

开发者 https://www.devze.com 2022-12-22 22:15 出处:网络
I have an applic开发者_如何转开发ation that passes in java.util.Date. I want to check whether this date is within a specified time of day (e.g. between 10:30 & 11:30), I don\'t care about the date

I have an applic开发者_如何转开发ation that passes in java.util.Date. I want to check whether this date is within a specified time of day (e.g. between 10:30 & 11:30), I don't care about the date, just the time of day.

Can anyone show me a simple way to do this?

Thanks


This is what the Calendar class is for. Assuming date is your Date object:

Calendar cal = Calendar.getInstance();
cal.setTime(date);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minutes = cal.get(Calendar.MINUTE);
if (hour == 10 && minutes >= 30 || hour == 11 && minutes <= 30) {
   ... 
}
0

精彩评论

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