Home > Enterprise >  Force JVM to start with different date different than machine time
Force JVM to start with different date different than machine time

Time:01-11

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

    }

}

CodePudding user response:

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

CodePudding user response:

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

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

  •  Tags:  
  • Related