Johan
Johan

Reputation: 2159

java/android datetime timespan

If i have a simpledateformat 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

Upvotes: 2

Views: 2170

Answers (3)

ashokgelal
ashokgelal

Reputation: 81538

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

Upvotes: 1

mdelolmo
mdelolmo

Reputation: 6447

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.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240908

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
}

Upvotes: 5

Related Questions