spring boot配置时区
项目配置时区为中国上海:
import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import simple.proj.zxz.play.prop.CommProp; import javax.annotation.PostConstruct; import java.util.TimeZone; @SpringBootApplication @Slf4j public class PlayApplication { @Autowired private CommProp commProp; @PostConstruct void started() { //时区设置:中国上海 //time.zone: "Asia/Shanghai" TimeZone.setDefault(TimeZone.getTimeZone(commProp.getTimeZone())); } public static void main(String[] args) { log.info("*** starting application PLAY ***"); SpringApplication.run(PlayApplication.class, args); log.info("*** application PLAY started ***"); } }
如果想要设置其他时区,可以先查看所有支持的时区设置:
import first.zxz.tools.PrintTool; import java.util.TimeZone; /** * 测试类 * * @author zhangxz * 2019/11/6 */ public class Test { public static void main(String[] args) { System.out.println("可选得时区总数量:" + TimeZone.getAvailableIDs().length); PrintTool.printStringArr(TimeZone.getAvailableIDs()); // System.out.println(TimeZone.getTimeZone("Asia/Shanghai")); // System.out.println(TimeZone.getTimeZone("CTT")); } }
结束