Manuel Perez Manuel Perez - 2 months ago 31
Java Question

How to Compare sysdate time and String time in Java

want i want to do is if the time is 10:00:00 proc.process will be executed and if the time is 11:00:00 proc.process1 will be executed

String TimeVal = "11:00:00";
String TimeVal1 = "12:00:00";

DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
Date date1 = sdf.parse(TimeVal);
Date date2 = sdf2.parse(TimeVal1);

if( date1 == date ){
proc.process(variable);
}
if(date1==date){
proc.process1(variable);

proc.process2(variable);
proc.process3(variable);
proc.process4(variable);
proc.process5(variable);
proc.process6(variable);
proc.process7(variable);
proc.process8(variable);
proc.process9(variable);
proc.process10(variable);
proc.process11(variable);

Answer

Why don't you simply use the java 8 date api ?

Like :

 LocalTime firstTime = LocalTime.of(11, 0, 0); 
 LocalTime secondTime = LocalTime.of(12, 0, 0);

 if (LocalTime.now().isAfter>= startTime &&
     LocalTime.now().isBefore<= endTime){
     //process
 }