Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default Job Scheduling in java

    I am trying to schedule a job using java. By using timer class i can able to specify to schedule at particular time. But I am looking to specify between two dates and also need to specify the time between the two dates. Could someone help me to say how we can use timer class and schedule the task between two dates ?

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Properties;
    import java.util.Timer;
    import java.util.TimerTask;

    public class Scheduler {

    public static Object[] getInput(){
    Properties properties = new Properties();
    try {
    properties.load(new FileInputStream("./property/SchedulerProperties.properties"));
    System.out.println(properties);
    } catch (IOException e) {
    e.printStackTrace();
    }

    String[] datetime = properties.getProperty("startdatetime").split(":") ;

    Calendar date = Calendar.getInstance();
    Calendar now = Calendar.getInstance();

    date.set(Calendar.DATE, Integer.parseInt(datetime[0]));
    date.set(Calendar.MONTH, Integer.parseInt(datetime[1]));
    date.set(Calendar.YEAR, Integer.parseInt(datetime[2]));

    date.set(Calendar.HOUR, Integer.parseInt(datetime[3]));
    date.set(Calendar.MINUTE, Integer.parseInt(datetime[4]));
    date.set(Calendar.SECOND, Integer.parseInt(datetime[5]));
    date.set(Calendar.MILLISECOND, Integer.parseInt(datetime[6]));

    // Schedule to run every Sunday in midnight
    // date.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
    // you can find lot of other options for days and months in calendar object

    if(date.before(now)){
    date = now;
    }

    long delaySeconds = Long.parseLong(properties.getProperty("delaysecond s"));
    Long delayInSeconds = new Long(delaySeconds);
    return new Object[]{date.getTime(),delayInSeconds};
    }

    public void schedule(TimerTask tt){
    Timer timer = new Timer();
    Object[] input = getInput();
    System.out.println((Long)input[1]);
    // timer.schedule(tt,(Date)input[0],(Long)input[1]);
    timer.schedule(tt,(Date)input[0]);
    //timer.schedule(tt,(Date)input[0],delaySeconds);
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    the time between the two dates
    Create two calendar objects, one for each date. Use the getTime method on each and subtract.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. * Help: Java Code For Cpu Scheduling Algorithm
    By coldvoice05 in forum AWT / Swing
    Replies: 2
    Last Post: 03-10-2010, 03:43 PM
  2. * Help: Java Code For Cpu Scheduling Algorithm
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 07-15-2009, 04:30 AM
  3. Quartz scheduling in EJB3
    By Niveditha in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 06-30-2008, 08:34 AM
  4. Scheduling tasks using Threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10:24 AM
  5. Scheduling a task
    By bugger in forum Advanced Java
    Replies: 3
    Last Post: 12-04-2007, 12:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •