// Here is the implementation that helps to set a particular process not to run between 12 am to 7 am
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
if(!checkTimeFrame()) {
System.out.println("Do not run the process - since time is between 12 am and 7 am");
}
}
public static boolean checkTimeFrame(){
Date currentDateTime =todaysDateTime();
System.out.println("currentDateTime: " + currentDateTime);
Date t=stringDateToUtilDate(todaysDate());
System.out.println("Before adding 7 hours t: " + t);
t.setTime(t.getTime()+25200000); // 25200000 = 7 X 60 X 60 X 1000 = 7 hours
System.out.println("After adding 7 hours t: " + t);
System.out.println("Is current time later than 7 AM: " + currentDateTime.after(t));
return currentDateTime.after(t);
}
public static Date todaysDateTime() {
return new Date() ;
}
// Since we are formatting the date, time is lost. Only the date component is retained.
public static String todaysDate() {
Date currentSystemdate = new Date();
SimpleDateFormat formater = new SimpleDateFormat("MM/dd/yyyy");
String stringDate = formater.format(currentSystemdate);
System.out.println("Formatted (\"MM/dd/yyyy\") date: " + stringDate);
return stringDate ;
}
public static Date stringDateToUtilDate(String stringDate) {
java.util.Date utilDate = null;
try{
if (null != stringDate && !(stringDate.trim().equals("")))
{
SimpleDateFormat formattedDate = new SimpleDateFormat("MM/dd/yyyy");
utilDate = formattedDate.parse(stringDate);
System.out.println("String to util date: " + utilDate);
}
}catch(ParseException exception){exception.printStackTrace();}
catch(Exception exception){exception.printStackTrace();}
return utilDate;
}
}