开发者

Check if date is within time range using Joda or standard Java API

开发者 https://www.devze.com 2022-12-31 18:09 出处:网络
I have first time a开发者_开发问答s string \'12:00:00\' and another \'19:00:00\'. How to check time portion of the day is within these time values?Using Joda Time you could do something like this:

I have first time a开发者_开发问答s string '12:00:00' and another '19:00:00'. How to check time portion of the day is within these time values?


Using Joda Time you could do something like this:

DateTime start = new DateTime(2010, 5, 25, 12, 0, 0, 0);
DateTime end = new DateTime(2010, 5, 25, 21, 0, 0, 0);
Interval interval = new Interval(start, end);
DateTime test = new DateTime(2010, 5, 25, 16, 0, 0, 0);
System.out.println(interval.contains(test));


Create calendar instances of the start and end times for the same day as your 'test' date. Then you can easily compare the calendar objects to determine if the current date is inside or outside the range.

0

精彩评论

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

关注公众号