TimerTask not executing

JAN :

I am programming a discord bot and want to run an unban at a certain time. However, the TimerTask is not called even though the date is in the future. If the date is in the past, the timertask calls instant. I've tried to sout some data to look if the date is wrong but its right.

I want to call it once.

        System.out.println("started");
        System.out.println(date);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("called");
                user.openPrivateChannel().complete().sendMessage(
                        "**-Unban-**" +
                                "\n" +
                                "Du wurdest entbannt!"
                ).queue();
                guild.removeRoleFromMember(user.getId(), guild.getRoleById("690579286582624276")).queue();
                guild.addRoleToMember(user.getId(), guild.getRoleById("688733671104053327")).queue();
            }
        }, date);

Output from Console:

started
Fri Mar 20 17:21:55 UTC 2020

Hope you'll find out!

Arvind Kumar Avinash :
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Main {
    Date date = new Date();// Initialize it as per your requirement
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            System.out.println("called");
            //...
            timer.cancel();
        }
    };

    public void start() {
        timer.schedule(task, date);
    }

    public static void main(String[] args) {
        Main timer = new Main();
        timer.start();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=341350&siteId=1