2

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

Johan
  • 2,149
  • 4
  • 21
  • 18

3 Answers3

5
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
}
jmj
  • 237,923
  • 42
  • 401
  • 438
1

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.

mdelolmo
  • 6,417
  • 3
  • 40
  • 58
1

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

Community
  • 1
  • 1
ashokgelal
  • 80,002
  • 26
  • 71
  • 84