javakotlinbukkittimertask

Running a method every day at a specific time with Timer()


Using Kotlin, Bukkit (Spigot), and Timer() (Or anything that also helps), I'm trying to create a way that I can run another method, every day at a specific time.

Here's what I have so far, which doesn't work.

fun schedule() {
        val timer = Timer()
        val format = SimpleDateFormat("hh:mm:ss") 
        val date = format.parse("11:07:09")
        timer.schedule(sendMessage(), format, date)
}

fun sendMessage() {
    System.out.println("Test");
}

Doesn't work because timer.schedule() requires a TimerTask, Date, and a long.

What I'm confused about, how do I convert format and date so add it to timer.schedule() so this does run every day? Also, how would I respect timezones, and make sure this runs at-least near the server time?


Solution

  • val timer = Timer()
    val task: TimerTask = object : TimerTask() {
        override fun run() {
           // do your task here
        }
    }
    // repeat every hour
    timer.schedule(task, 0L, 1000 * 60 * 60)
    

    As refer to here.