Jump to content

Having a java function run every day at a certain time.

Go to solution Solved by BobVonBob,
3 minutes ago, DriedSponge said:

I need it to run at a specific time of day.

In that case, I'm completely stumped why this prior code didn't work. Are you sure you had the hour and minute set after the local time of your computer when you ran it?

 

class TimerThing {
    public static void main() {
        Timer timer = new Timer();
        Calendar date = Calendar.getInstance();
        date.set(Calendar.HOUR, 13);
        date.set(Calendar.MINUTE, 21);
        date.set(Calendar.SECOND, 0);
        date.set(Calendar.MILLISECOND, 0);
        timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        System.out.println("Test");
                    }
                },
                date.getTime(),
                1000 * 60 * 60 * 24
        );
    }
}

 

Well I guess that was the issue. I'm not sure if it had to do with the fact that I'm on windows but it works now.

Thanks @BobVonBob  for all the help. I really appreciate it!

Remember to quote or @mention others, so they are notified of your reply

AMD Ryzen 9 5950X | Arctic Liquid Freezer II 360 | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 32 GB DDR4-3600 | XFX Swift AMD Radeon RX 9070XT

Samsung 980 EVO Plus 2TB | SK hynix Gold S31 500GB SSD | Seagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 2TB Samsung 970 EVO Plus NVMe SSD | 3x Phanteks T30-120

Corsair RM1000e 1000 W 80+ Gold Certified Modular PSU | Corsair 5000D Airflow Windows 11 Home

Link to post
Share on other sites

1 minute ago, DriedSponge said:

Well I guess that was the issue. I'm not sure if it had to do with the fact that I'm on windows but it works now.

Thanks @BobVonBob  for all the help. I really appreciate it!

Nope, instead of setting Calendar.HOUR, you should set Calendar.HOUR_OF_DAY. I totally missed this before, and finally gave up and tried the code locally. That was the problem.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×