no

How to set the default timezone and locale in Spring Boot

This piece of code sets the default timezone and locale in Spring Boot. These are very useful, specially in development. For example, if you...

This piece of code sets the default timezone and locale in Spring Boot. These are very useful, specially in development. For example, if you did not set the timezone and you have developers working on different time zones then the tests that run on one time zone might fail on another.

package com.czetsuyatech;

import java.time.ZoneOffset;
import java.util.Locale;
import java.util.TimeZone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BackendApplication {

  private static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone(ZoneOffset.UTC);
  private static final Locale DEFAULT_LOCALE = Locale.ENGLISH;

  public static void main(String[] args) {

    init();
    SpringApplication.run(BackendApplication.class, args);
  }
  
  private static void init() {
    TimeZone.setDefault(DEFAULT_TIME_ZONE);
    Locale.setDefault(DEFAULT_LOCALE);
  }
}

Related

spring-rest 3198929746359617162

Post a Comment Default Comments

item