0

I am using spring boot and @schedule to scheduled some cron jobs to start at specific time ; however , I want to write code to be able to manipulate date and time in my spring boot application in order to test whether the cron job will be triggered or not , I know I can do this through change machine (OS) date , but I want to this through my application startup . I remember I've seen it somewhere , but I couldn't find it again .

@EnableScheduling
@SpringBootApplication
public class MainApplication {


    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

    }

}
Omar Abo Elsoud
  • 154
  • 2
  • 12

2 Answers2

0

You wont be able to change the system time in JVM. You have two options to test.

  1. stack overflow thread to change the system time from Java code. This changes your machine time as well. How can I set the System Time in Java?

  2. stack overflow thread to test the schedule annotation How to test Spring @Scheduled

-1

You can change the timezone of the application by using System.setProperty() method. Below is the example.

System.setProperty("user.timezone", "Asia/Kolkata");

Durgesh
  • 36
  • 3
  • I want to change the time to specific date and hour .for example , today is 11/1 at 1:01 pm , I want to my application start with 30/1/2022 and time 10:30 am . SetProperty will not achieve my goal – Omar Abo Elsoud Jan 11 '22 at 11:01