I am using Java Timer class for running certain task. For my program I will give starttime and endtime as parameters which will ask the java timer when to start and when to end the task.
While running my task, I want to get and check the difference between current time and endtime periodically and If current time equals endtime then cancel the task by calling timer.cancel()
method.
My startTime
and endTime
parameters time format are like this
String startTime = "12:00";
String endTime = "12:01";
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
Date date1 = format.parse(startTime);
Date date2 = format.parse(endTime);
long difference = date2.getTime() - date1.getTime();
I want to check the current time is equal to endtime.
But I want to check the condition that the current time is equal to end time and call the timer.cancel()
May be, you just don't know how to get the endTime.
String startTime = "12:00";
SimpleDateFormat sDateFormat = new SimpleDateFormat(
"hh:mm");
String endTime = sDateFormat.format(new java.util.Date());
if (startTime.equals(endTime)) {
timer.cancel();
}
try, and good luck