2

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?

kinx
  • 463
  • 5
  • 12
  • 31
  • Take a look this one: https://stackoverflow.com/questions/9375882/how-i-can-run-my-timertask-everyday-2-pm – Timur Levadny Oct 28 '18 at 14:28
  • @TimurLevadny Yeah I saw this question, and I tried to convert it to what I was doing, but with it not having the exact format I need (hh:mm:ss, which will be grabbed from a file) and the formatted date (11:07:09), it's confusing to use. – kinx Oct 28 '18 at 14:36
  • A nice practice to consider is to define an AWS lambda function as a timer that will trigger an endpoint you provide (if AWS lambda is available to you...) – Lior Bar-On Oct 28 '18 at 15:27
  • @LiorBar-On not sure how do anything you said there. – kinx Oct 28 '18 at 15:33
  • 1
    Okay. Maybe you better create a new, more specific questions, about date conversion. It will be easier to answer than this. – Lior Bar-On Oct 28 '18 at 15:34

1 Answers1

2
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.

Skully
  • 2,882
  • 3
  • 20
  • 31
scr
  • 134
  • 2
  • 7