So i want to schedule a task on a specific time like 7:15AM and a want to execute it once, i want to work with Quartz but it makes the task execute every period of time (5 minutes for example), but i don't want this i want to schedule the task based on real time. Thank you
-
What do you mean by "execute it once"? Only at this time towmorrow but not ever after? – Aug 15 '19 at 06:53
-
1Take a look at http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/tutorial-lesson-06.html – Aug 15 '19 at 06:54
-
i mean by "once" is that i want to execute it 1 time (not repeatedly). – Foued Sedrati Aug 15 '19 at 06:59
-
Only once, never after? – Aug 15 '19 at 07:01
-
2Possible duplicate of [How do I schedule a task to run once?](https://stackoverflow.com/questions/34324082/how-do-i-schedule-a-task-to-run-once) – Aug 15 '19 at 07:02
-
Just a guess may help https://docs.oracle.com/javaee/7/api/javax/ejb/Schedule.html – Dushyant Tankariya Aug 15 '19 at 07:02
-
am sorry i checked my program and i need it to run every day so problem solved thanks – Foued Sedrati Aug 15 '19 at 07:06
4 Answers
Please use CronTrigger expression.eg: 0 15 07 * * *

- 1
-
1Can you explain that further? How does this "CronTrigger expression" ensure that the task is run **exactly once**? – Nico Haase Aug 15 '19 at 08:00
and a want to execute it once,
Some old code of mine that fires off one-off scheduled jobs
Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
cal.setTime(df.parse(job.getRunatDate()));
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(job.getRunatHour()));
cal.set(Calendar.MINUTE, Integer.parseInt(job.getRunatMin()));
log.debug ("One-off job at " + cal.getTime());
Trigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger()
.withIdentity("trigger" + job.getLineNumber(), "group1")
.startAt(cal.getTime())
.build();
sched = new StdSchedulerFactory().getScheduler();
sched.start();
sched.scheduleJob(job, trigger);
edit
As Basic mentions, this is using very old date-time classes that was replaced by JSR310. Either use that or the Three10 Backport

- 44,617
- 6
- 35
- 64
-
These terrible date-time classes were supplant d years ago by the *java.time* classes with the adoption of JSR 310. – Basil Bourque Aug 15 '19 at 07:27
-
@BasilBourque Yeah, I did mention that it was **old** code. 2014, if that is any excuse. – Scary Wombat Aug 15 '19 at 07:29
Get the current moment as seen in your desired time zone.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdtNow = ZonedDateTime.now( z ) ;
Define your target time.
LocalTime targetTime = LocalTime.of( 7 , 15 ) ;
Compare to current time.
LocalTime timeNow = zdtNow.toLocalTime() ;
ZonedDateTime targetZdt = zdtNow.with( targetTime ) ;
if( ! timeNow.isbefore( targetTime ) )
{
targetZdt = targetZdt.plusDays( 1 ) ; // Auto-adjusts if this time of day is not valid on that date in that zone.
}
Calculate elapsed time until then.
Duration d = Duration.between( zdtNow , targetZdt ) ;
Use the Executors framework to schedule your Runnable task with a single-threaded ScheduledExcutorService
after waiting the amount of time in the Duration. Search Stack Overflow as this has been covered many times.

- 303,325
- 100
- 852
- 1,154
Thank you guys for the help i found an answer here:
CronTrigger Example 1 - an expression to create a trigger that simply fires every 5 minutes
“0 0/5 * * * ?”
CronTrigger Example 2 - an expression to create a trigger that fires every 5 minutes, at 10 seconds after the minute (i.e. 10:00:10 am, 10:05:10 am, etc.).
“10 0/5 * * * ?”
CronTrigger Example 3 - an expression to create a trigger that fires at 10:30, 11:30, 12:30, and 13:30, on every Wednesday and Friday.
“0 30 10-13 ? * WED,FRI”
CronTrigger Example 4 - an expression to create a trigger that fires every half hour between the hours of 8 am and 10 am on the 5th and 20th of every month. Note that the trigger will NOT fire at 10:00 am, just at 8:00, 8:30, 9:00 and 9:30
“0 0/30 8-9 5,20 * ?”
Note that some scheduling requirements are too complicated to express with a single trigger - such as “every 5 minutes between 9:00 am and 10:00 am, and every 20 minutes between 1:00 pm and 10:00 pm”. The solution in this scenario is to simply create two triggers, and register both of them to run the same job.

- 3
- 4