項目配置時區為中國上海:
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")); } }
結束