开发者

java/android datetime timespan

开发者 https://www.devze.com 2023-02-04 15:24 出处:网络
If i have a simpledatef开发者_如何转开发ormat object: SimpleDateFormat format = new SimpleDateFormat(\"HH:mm\");

If i have a simpledatef开发者_如何转开发ormat object:

SimpleDateFormat format = new SimpleDateFormat("HH:mm");

And have two times, 12:12 and 13:13 for example, is there an easy way to check if the current time is between the two SimpleDateFormats (The clock is 12:15 for example)? Thanks


DateFormat newDateFormat = new SimpleDateFormat("HH:mm");    

Date d1 = newDateFormat.parse("12:12");
Date d2 = newDateFormat.parse("13:13");
Date myDate = newDateFormat.parse("12:15");

if(myDate.getTime() >= d1.getTime() && myDate.getTime() <= d2.getTime()){
//yes it is in between  including border
}


Compare them using Date or Calendar objects, SimpleDateFormat is just a representation. You can use format.parse("12:12") and ir returns a Date object representing that time.


If anyone is looking for a better and lighter library to use in Android/ Java that handles interval better, you can use a library that a wrote to use in my own Android app. https://github.com/ashokgelal/samaya

Also, see this question: Do we have a TimeSpan sort of class in Java

0

精彩评论

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