运行环境新建测试类(TestDate.java)
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class TestDate {
public static void main(String[] args) {
System.out.println("获取时间"+new Date());
Date d = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");
System.out.println("格式化输出:"+sdf.format(d));
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
System.out.println("Asia/Shanghai:" +sdf.format(d));
}
}
运行环境执行
/app # javac TestDate.java
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=lcd
/app # java TestDate
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=lcd
get JVM dateMon Oct 23 03:32:29 GMT 2017
jvm date format2017-10-23 03:32:29
Asia/Shanghai:2017-10-23 11:32:29
/app #
java虚拟机获取的时区不是中国时区,应该增加8小时
上述是在代码层面处理
启动时候增加时区设置参数-Duser.timezone=GMT+08
command: [java,'-Duser.timezone=GMT+08','-Djava.security.egd=file:/dev/./urandom', -Xmx200m, -jar, /app/app.jar]