I have a back-end job that runs once a day and is scheduled at a particular time based on the entity/user etc.
Now, I want to validate if the current time is earlier than the job, if yes, then job can be rescheduled, else if job has already passed, then of course it cant be rescheduled for the day.
public static String getCurrentTime(String format) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(cal.getTime());
}
String time = getCurrentTime("yyyy-MM-dd HH:mm:ss");
String backendJobTime = getBackendJobSchedule();
String[] onlyTime = time.split("\\s+");
String time = onlyTime[1];
Integer.parseInt(time);
Same for back-end job
if(time < backendJob){
system.out.println("Job is yet to be exectued...):
}
Now,Ii want to get substring of time and then compare with other time of the back-end job and see if it's earlier. I can't write the complete logic.
tl;dr
Details
Never use the terrible legacy date-time classes
Date,Calendar,SimpleDateFormat. These were supplanted years ago by the modern java.time classes defined in JSR 310.You said:
You need more than a date and time-of-day to track a moment. For a specific point on the time line, you need the context of a time zone or offset from UTC.
In Java, track a moment as an
Instantobject. This class represents a moment as seen with an offset of zero hours-minutes-seconds from UTC.To serialize as text, use standard ISO 8601 formats.
The
Zindicates an offset of zero. Pronounced “Zulu”.And parse:
Compare by calling
isBefore,isAfter, andequals.Notice that no time zone is involved in the code above. Perhaps you want to set the target time per your own time zone.
Instantiate a
ZonedDateTimeobject. Extract anInstantto adjust to UTC (offset of zero).