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
        );
    }
}

 

How can I run a java method at a set time each day? I took a look at this stack overflow thread but I couldn't get it to work. I woud like to find a way to do it without using an external scheduler. Thanks in advance. 

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

6 minutes ago, DriedSponge said:

How can I run a java method at a set time each day? I took a look at this stack overflow thread but I couldn't get it to work. I woud like to find a way to do it without using an external scheduler. Thanks in advance. 

What exactly did you try to get it to work, can you share some code?

 

Timer/TimeTask should be the way to do it. Naturally the Java program itself needs to be running the whole time. So you can't quit the program for it to be able to run the method at the required time. Otherwise you will need an external timer if the program isn't supposed to run continuously.

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

Link to post
Share on other sites

If this is a tool just for you or something you'll always have control over I'd suggest using an external scheduler instead of trying to schedule things within the program, since that reduces the burden on you to avoid bugs, make sure the program is always running and/or can resume after not running when it should, etc. If this is something going out to end users a more robust scheduling solution like Quartz might be a good choice.

¯\_(ツ)_/¯

 

 

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

13 minutes ago, Eigenvektor said:

What exactly did you try to get it to work, can you share some code?

 

Timer/TimeTask should be the way to do it. Naturally the Java program itself needs to be running the whole time. So you can't quit the program for it to be able to run the method at the required time. Otherwise you will need an external timer if the program isn't supposed to run continuously.

Here is my code:
(they don't have java syntax highlighting so I just did C# highlighting

import org.bukkit.Bukkit;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Broadcast extends TimerTask{
    public void run() {
        Bukkit.broadcastMessage("Test");
    }
}
class MainTimer{
    public static void main(String[] args) {
        Timer timer = new Timer();
        Calendar date = Calendar.getInstance();
        date.set(
                Calendar.DAY_OF_WEEK,
                Calendar.SATURDAY
        );
        date.set(Calendar.HOUR, 23);
        date.set(Calendar.MINUTE, 38);
        date.set(Calendar.SECOND, 0);
        date.set(Calendar.MILLISECOND, 0);
        timer.schedule(
                new Broadcast(),
                date.getTime(),
                1000 * 60 * 60 * 24 * 7
        );
    }
}

 

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

16 minutes ago, Eigenvektor said:

Naturally the Java program itself needs to be running the whole time. So you can't quit the program for it to be able to run the method at the required time. Otherwise you will need an external timer if the program isn't supposed to run continuously.

 

5 minutes ago, BobVonBob said:

If this is a tool just for you or something you'll always have control over I'd suggest using an external scheduler instead of trying to schedule things within the program, since that reduces the burden on you to avoid bugs, make sure the program is always running and/or can resume after not running when it should, etc. If this is something going out to end users a more robust scheduling solution like Quartz might be a good choice.

It's a plugin for my minecraft server which should be runnign 24/7 except for the ocasional server restart.

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

It looks to me like based on that code snippet it should work, you've just scheduled the task for every Saturday at 23:38. If you just want to run it once a day and don't care when it runs don't bother setting date and remove the * 7 in the timer since that's making it run once a week. Shorter intervals also recommended for testing.

¯\_(ツ)_/¯

 

 

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

20 minutes ago, BobVonBob said:

It looks to me like based on that code snippet it should work, you've just scheduled the task for every Saturday at 23:38. If you just want to run it once a day and don't care when it runs don't bother setting the time and remove the * 7 in the timer since that's making it run once a week. Shorter intervals also recommended for testing.

Agreed. I would also recommend to use the Java 8 APIs where possible, since it can make the code more readable. Instead of multiplying values, use more descriptive code like "Duration":

final Timer timer = new Timer();
timer.schedule(
	new TimerTask() {
		@Override
		public void run() {
			System.out.println("Test");
		}
	},
	Date.from(Instant.now()),
	Duration.ofSeconds(10).toMillis()
);

This prints "Test" on the command line when the code is started and every 10 seconds afterwards. To get it to run every week you could then use "Duration.ofDays(7).toMillis()" instead.

 

~edit: Here's another way of doing it:

// Next run at midnight (UTC) - Replace with local time zone, if needed
final ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
ZonedDateTime nextRun = now.withHour(0).withMinute(0).withSecond(0);

// If midnight is in the past, add one day
if (now.compareTo(nextRun) > 0) {
	nextRun = nextRun.plusDays(1);
}

// Get duration between now and midnight
final Duration initialDelay = Duration.between(now, nextRun);

// Schedule a task to run at midnight and then every day
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(() -> System.out.println("Test"),
		initialDelay.toMillis(),
		Duration.ofDays(1).toMillis(),
		TimeUnit.MILLISECONDS);

// Print time to midnight (UTC!), for debugging
System.out.println("Time until first run: " + initialDelay);

 

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

Link to post
Share on other sites

2 hours ago, BobVonBob said:

It looks to me like based on that code snippet it should work, you've just scheduled the task for every Saturday at 23:38. If you just want to run it once a day and don't care when it runs don't bother setting the time and remove the * 7 in the timer since that's making it run once a week. Shorter intervals also recommended for testing.

Yeah I did that and got it to work when running once every second. However, when trying to get it to run at a specific time of day, I could not get it to work.

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

Here is the code I have that makes it run every second
 

class TimerThing {
    public static void main() {
        Timer timer = new Timer();
        Calendar date = Calendar.getInstance();
        date.set(Calendar.HOUR, 13);
        date.set(Calendar.MINUTE, 17);
        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
        );
    }
}

And this is the code I use to get it to run at a specified time:
 

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
        );
    }
}

 

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

Is it just not running the first time? Try a shorter timer, say 10 seconds, with date in the future and see when the function first runs.

¯\_(ツ)_/¯

 

 

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

Just now, Mortenrb said:

Yeahno... if it's in the same namespace, you should name the classes differently (e.g. TimerThingSecond and TimeThingDay).

But the optimal route would be to have one generic timer for both times.

They're the same code, the first is a test to get the timer working, the second was an attempt at getting the task to run at a specific time.

¯\_(ツ)_/¯

 

 

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

11 minutes ago, BobVonBob said:

Is it just not running the first time? Try a shorter timer, say 10 seconds, with date in the future and see when the function first runs.

Ok this seems to work for 10 seconds. I'm not sure if it's the proper way to do it though.
 

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, 10);
        date.set(Calendar.MILLISECOND, 0);
        timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        System.out.println("Test");
                    }
                },
                date.getTime(),
                1000 * 10
        );
    }
}

 

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

Here is a part of the log:
 

[14:02:10] [Timer-3/INFO]: Test
[14:02:20] [Timer-3/INFO]: Test
[14:02:30] [Timer-3/INFO]: Test
[14:02:35] [Timer-2/INFO]: Test
[14:02:40] [Timer-3/INFO]: Test
[14:02:50] [Timer-3/INFO]: Test
[14:03:00] [Timer-3/INFO]: Test
[14:03:10] [Timer-3/INFO]: Test
[14:03:20] [Timer-3/INFO]: Test
[14:03:30] [Timer-3/INFO]: Test
[14:03:35] [Timer-2/INFO]: Test
[14:03:40] [Timer-3/INFO]: Test
[14:03:50] [Timer-3/INFO]: Test
[14:04:00] [Timer-3/INFO]: Test
[14:04:10] [Timer-3/INFO]: Test
[14:04:20] [Timer-3/INFO]: Test
[14:04:30] [Timer-3/INFO]: Test
[14:04:35] [Timer-2/INFO]: Test
[14:04:40] [Timer-3/INFO]: Test
[14:04:50] [Timer-3/INFO]: Test
[14:05:00] [Timer-3/INFO]: Test
[14:05:10] [Timer-3/INFO]: Test

 

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

I meant to set a specific minute in date, then see if the TimerTask runs on that minute or if it starts 10 seconds later.

¯\_(ツ)_/¯

 

 

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

3 minutes ago, BobVonBob said:

I meant to set a specific minute in date, then see if the TimerTask runs on that minute or if it starts 10 seconds later.

I set it to run on minute 20 at 10 seconds and it seem to have worked
Code:

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

Response

[14:20:10] [Timer-0/INFO]: Test

 

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

That's weird. It has also happend at other times:
 

[14:20:10] [Timer-0/INFO]: Test
[14:23:30] [Timer-0/INFO]: Test
[14:26:50] [Timer-0/INFO]: Test
[14:30:10] [Timer-0/INFO]: Test

 

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

The time in timer.schedule doesn't need to match the date you set, it's the time in ms between each time the TimerTask runs. Currently it's 200,000 ms, or 200 seconds between runs.

 

Do you need this to run at specific times or just once a day? If it's just every 24 hours after you start the script you can delete everything changing the time after getInstance, since getInstance fills the Calendar object with the current system time.

¯\_(ツ)_/¯

 

 

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

1 minute ago, BobVonBob said:

The time in timer.schedule doesn't need to match the date you set, it's the time in ms between each time the TimerTask runs. Currently it's 200,000 ms, or 200 seconds between runs.

 

Do you need this to run at specific times or just once a day? If it's just every 24 hours after you start the script you can delete everything changing the time after getInstance, since getInstance fills the Calendar object with the current system time.

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

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

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
        );
    }
}

 

¯\_(ツ)_/¯

 

 

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

24 minutes ago, BobVonBob said:

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?

Yeah I have no idea why eiter, I'm 100% sure I had  the hour and minute set after my local time.

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

@BobVonBob

It's running now, it's just off by 50 seconds.
Code:

class TimerThing {
    public static void main() {
        Timer timer = new Timer();
        Calendar date = Calendar.getInstance();
        date.set(Calendar.HOUR, 15);
        date.set(Calendar.MINUTE, 16);
        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
        );
    }
}

Response:

[15:16:50] [Timer-0/INFO]: Test

 

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

4 minutes ago, DriedSponge said:

@BobVonBob

It's running now, it's just off by 50 seconds.

Code:

-snip-

Response:


[15:16:50] [Timer-0/INFO]: Test

 

That's just the old 200 second timer again.

¯\_(ツ)_/¯

 

 

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

29 minutes ago, BobVonBob said:

That's just the old 200 second timer again.

Yeah I figured. I just tried it again on the original timer and it did not work. This is quite a strange issue.

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

40 minutes ago, BobVonBob said:

That's just the old 200 second timer again.

I think I found the solution. For some reason it wants me to do 4 instead of 16.

class TimerThing {
    public static void main() {
        Timer timer = new Timer();
        Calendar date = Calendar.getInstance();
        date.set(Calendar.HOUR, 4);
        date.set(Calendar.MINUTE, 6);
        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
        );
    }
}

 

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

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

×