I am developing a Java application that I want to play the Azan file at the five Islamic religious times, which are different for each day and are calculated according to astronomical calculations for each day. For this, it is necessary for the running program to continuously check what day and time it is, and for example, if the Azan time is 12:15 today, the Azan file should be played, and so on for other times and days.
I tried to use the Java Task Timer class for this purpose, but I have trouble running the task at the desired times and the program does not run properly. I have given the sample program code below.
package org.example;
import java.time.LocalTime;
import java.util.Timer;
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
@Override
public void run() {
//CACLULATE THE TIMES FUNCTION();
LocalTime now = LocalTime.now();
LocalTime morning = LocalTime.parse( "5:32" );
LocalTime noon = LocalTime.parse( "12:15" );
LocalTime afternoon = LocalTime.parse( "15:18" );
LocalTime night = LocalTime.parse( "17:59" );
switch(now){
case morning:
System.out.println( "good morning!" );
hit = true;
break;
case noon:
System.out.println( "good noon!" );
hit=true;
break;
case afternoon:
System.out.println( "good afternoon!" );
hit = true;
break;
case nightT:
System.out.println( "good night!" );
hit = true;
break;
default:
System.out.println( "BAD TIME!" );
}
}
}
class TTest{
public static void main(String args[]){
MyTimerTask task = new MyTimerTask();
Timer timer = new Timer();
timer.schedule(task, 1000);
try{
Thread.sleep(5000);
}catch (InterruptedException e){}
}
}
Given that you said that the astronomical calculations need to be done on a daily basis, I wouldn't put the calculation inside the TimerTask, as you should only need to calculate it once per day and use those calculations for that day (disclaimer: not an Islam expert) instead of having each thread run the calculation every time you check.
edit:
Now that I think about it, you should be able to use ScheduledExecutorService together with LocalTime. These are not deprecated classes and should be preferred over the below suggestion.
The idea should be something like so:
ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();
LocalTime now = LocalTime.now();
LocalTime morning = LocalTime.of(5,32);
ex.schedule(() -> { /*azan tasks to run*/ }, Duration.between(now, morning).toMillis(), TimeUnit.MILLISECONDS);
when comparing the time at point of calculation to the time of Azan, you have a duration. You can then schedule the executor to run your task after the duration and do this for all the other tasks for the day, then sleep until the next day and repeat the cycle
Next, your code does not make full advantage of the Task scheduling provided by Timer, you can schedule the task at a specific Date (date time), note this is a deprecated class, so you will have to convert your LocalTime to Date.
class TTest{
public static void main(String args[]){
Timer timer = new Timer();
while(!Thread.isInterrupted()) {
// calculate morning Azan
LocalTime morningAzan = ...;
Date convertedMorningAzan = ...;
MyTimerTask task = new MyTimerTask(); //timer task for morning azan
timer.schedule(task, convertedMorningAzan);
//do the same for noon, afternoon and night
try{
//sleep until next day calculation time
}catch (InterruptedException e){}
}
}
}
Then, you wont even have to check that the duration is correct within your TimerTask, because Timer will take care of handling that it is the correct time you specify. You just need to play the Azan file so you specify that in the run() method of your task.