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){}
}
}
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){}
}
}
Share
Improve this question
edited Feb 6 at 10:51
Mark Rotteveel
109k226 gold badges155 silver badges219 bronze badges
asked Feb 6 at 6:55
mhm.mapmhm.map
1511 silver badge10 bronze badges
9
|
Show 4 more comments
2 Answers
Reset to default 4Avoid Timer
The Timer
and TimerTask
classes are legacy. They were supplanted by the Executors framework added to Java 5. So noted in the Javadoc.
ScheduledExecutorService
Define your task, playing the Azan file, as an implementation of Runnable
.
Instantiate a ScheduledExecutorService
. Keep around for repeated use. Be sure to shut it down gracefully before your app exits, otherwise its backing thread pool may continue running indefinitely like a zombie
Incompatible types. Found: 'java.time.LocalTime', required: 'char, byte, short, int, Character, Byte, Short, Integer, String, or an enum'
– pebble unit Commented Feb 6 at 7:08