This question already has answers here:
run a Java program in specific time
(2 answers)
Closed 9 years ago.
How can i implement code to do something when a specific system time is reached?
The only solution i have thought of is using a timer to "tick" every few minutes or hours to check if the specific time has been reached.
Are there any other better solutions ?
Thanks.
sorry if i had not been clear, i would be implementing the code inside my Java program, it is to clear records of a log before a new day is coming and save the records.
Example: Clear the current records and save these records at 23:59.
In pure Java, there is a Timer class. This is useful if you have a program running already. Or you are running a web app that is always up.
Another alternative is to use operating system (UNIX cron) and have it start the Java program at that time. This is useful if you don't meet the conditions for Timer.
You can just make a timer with a long duration. If the trigger time will be 350 minutes from now, there's no point having a timer poll every minute to see if the time is reached. Just set your timer to 350 minutes. Once it fires, remove the timer. This is called a one-shot timer. I can't answer how to specifically do this in Java, unfortunately.
If you are using Unix-like systems have a look at cron
If you are on Windows have a look at What is the Windows version of cron?
if i understand your question correctly, for unix, you can put your code in crontab and schedule it to run at specific system time. while for windows, you can use task scheduler. this is how we do it to run specific test scripts for nightly builds.
Quartz Scheduler Framework is an enterprise class framework that can be used as a Timer.
did you try Quartz Scheduler? , it is a powerful and advance scheduler framework, to help Java developer to scheduler a job to run at a specified date and time.click here for more
I think java.util.concurrent.ScheduledExecutorService is enough.
Related
I am just curious on if it would be possible to write a java application and then have it run automatically during a certain time of day??
For example: If I had a program that would like open a text file and write the current date in it and then close. Would it be possible for me to get it to run at say 8:00 A.M. everyday without me having to run it?
The simplest solution would be to use a batch job scheduler, eg. cron on *nix or Windows scheduler. It can easily trigger your program to run at the desired time.
Otherwise, you could write a long running Java program that detects when 8am has rolled around and perform your desired action.
Also you can check janos♦'s answer below to Executing a task at a particular time in the morning using ScheduledExecutorService, hope it will help good luck...
https://codereview.stackexchange.com/questions/63520/executing-a-task-at-a-particular-time-in-the-morning-using-scheduledexecutorserv
So, I am writing a Java code where I want the program to take content from the web every monday 12 am.
I found SO many answers telling me how to run a program for certain amount of time. I want to run the program and execute every monday morning.
I want to know if any one has ANY idea where to start?
I found of AlarmManager for android applications (which is sort what I want) but I want it for Java program not application.
Or is this even possible?
Sure! If you are on linux or mac, just set up a cronjob to run at 0 0 * * 1
It'd be something like:
java MyScript.java
as the task.
Try this:
Scheduled Tasks With Cron for Java
http://quartz-scheduler.org/ is the standard tool to setup cron-like jobs
Yes it is using Windows Task Manager!
My computer is turned on 7/24. However, it does gets locked. I have an app that includes Selenium and other APIs. I need it to execute everyday at say 6 AM. Is there any way I could do this. I heard about the Windows Scheduler. Is there any way to make it execute on its own? Or waht is the best way to do this. (I have Windows 7)
What you need is a Quartz scheduler. Please find the link for the same : http://quartz-scheduler.org/
Using quartz scheduler you can schedule a job to run everyday at 6 AM using cron expressions. You can create cron expressions using www.cronmaker.com
I want to implement a project where I check system timings whenever I am logged in.
If I enter the office and log in my system the I should get the time and also when I go for a break I just lock my PC and go so at that time time should stop and again when I login it should start.
Basically it should show me the total time I was logged in my computer/PC.
In java you can obtain the time by several ways. Two of them are :
instanciate a new Date object :
Date myDate = new Date() ;
using :
System.currentTimeMillis()
I think you should think of using a software dedicated to that instead of implementing this in Java.
What you could do is this:
Create a Java application that can
log the time somewhere, using the
System.currentTimeMillis() or new
Date() approach.
Create a scheduled task in windows
that runs each time the user locks,
unlocks, logs on and logs off the
system. This scheduled task should
run your application. It should be
as simple as calling a batch file
which in turn invokes your Java
application.
The application should use all the
times captured to calculate the
effective time.
You can use System.currentTimeMillis() (or System.nanoTime()) for the start and the end, and then calculate the difference.
This will give you the time when you start / close Java. Linking this to system startup / system shutdown can be done by launching the application on startup. But that depends on the OS.
I'm looking for an effective way to execute a method everyday at 3PM regardless of when the application was initially run or how long it has been running.
This must be done entirely from the application with no OS intervention (ex. Windows Task Scheduler)
I have been experimenting with java.util.Timer in varies configurations but I have had no success.
Any help would be appreciated.
Thanks.
You should take a look at Quartz which is a Java-based job scheduling system.
You will probably want to use something like the quartz engine it can do things like execute tasks that missed (like during a ahem crash) and it takes the work out of trying to manage threads.
For example if you use threads and put it to sleep and wake it up 86400 seconds (one day) later you will wake up and hour late (day = 82800 seconds) or early (day = 90000 seconds) on DST change over day, so be careful with whatever solution you choose
A built-in JDK way is to do what others suggested and first calculate :
currentTime - desiredTime
Then you can use something like a schedule executor to submit the tasks, and run them with a particular delay. This is far simpler than the options you have with frameworks like Quartz, but doesn't require an external dependency.
Also, you should always list which JDK you're using, so people can provide solutions for your version of the JDK.
You can start a thread that calculates the difference to the next 3pm and sleeps for that time. When it wakes up it executes the method and recalculates and sleeps. Is this what you meant?
As stated by others Quartz is a choice, with it you can do cron-like operations, jobs or triggers, here is a link on this subject: http://www.ibm.com/developerworks/java/library/j-quartz/index.html
Jcrontab
Jcrontab is a scheduler written in Java. The project objective is to provide a fully functional schedules for Java projects.