Saturday, February 16, 2019

Database Timezone issue -Solution in java

We have at least once in our development has encountered one issue with database and client location is Timezone difference. 



In most of the cases, Our Backend /Application server is in a different timezone and our client server is in a different timezone. So what actually the problem happens is the difference in data time because of Timezone.

Also Learn: Method Overloading in java

Problem Statement - I have one table of order in my DB and I need to schedule the job to be run on specific time ( let suppose - 1:00 AM every day ) , the schedule is in IST timezone and the DB is in GMT+2 timezone. So the job runs 3.30 Hrs before the actual time it should run.



Solution - 
The Approach I have taken is to make sure that when the date in db is inserted it should be of the timezone where the scheduler is in.( Basically, both should be in same time zone .)

Technology /Programming language used -Java,, Spring boot , JPA

Now I have done a simple change in my code as below.


@SpringBootApplication
@EnableAsync
public class Application {


  @PostConstruct
   void started() {
     TimeZone.setDefault(TimeZone.getTimeZone("IST"));
   }
 
 public static void main(String[] args) {
  SpringApplication.run(Application.class, args);
  
 }
 
 
 
}

So in the above code, I am explicitly making the default timezone as IST for the db as well.

Hope this will help you in code.



If you face any issue, please leave us a comment.

Thanks for reading 
Noeik 

0 comments:

Post a Comment