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

}

结束


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM