I have two String variables – time1 and time2. Both contain value in the format HH:MM. How can I check:
- If the current time is within
time1andtime2? time1will happen in the nearest
hour?
Upd.
I’ve implemented the following to convert time1 to Date format. But it uses depreciated methods:
Date clTime1 = new Date();
SimpleDateFormat timeParser = new SimpleDateFormat("HH:mm", Locale.US);
try {
clTime1 = timeParser.parse(time1);
} catch (ParseException e) {
}
Calendar now = Calendar.getInstance();
clTime1.setYear(now.get(Calendar.YEAR) - 1900);
clTime1.setMonth(now.get(Calendar.MONTH));
clTime1.setDate(now.get(Calendar.DAY_OF_MONTH));
System.out.println(clTime1.toString());
Dateobjects (which are also time objects)
Create a new
Dateobject.contain the current time.
Date.before() and Date.after() methods to determine if
you are in the time interval.
EDIT: You should be able to use this directly (and no deprecated methods)