JAVA 獲取北京時間 親自驗證


網上搜了半天  好多代碼都是 要不代碼報錯  要不就是答得驢唇不對馬嘴  要不就是代碼不驗證就發上來的 壓根就是獲取本機時間

自己把本機的時區改成非北京 試下

這篇文章是正確的


Date date = new Date(1503544630000L);  // 對應的北京時間是2017-08-24 11:17:10
 
SimpleDateFormat bjSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     // 北京
bjSdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));  // 設置北京時區
 
SimpleDateFormat tokyoSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  // 東京
tokyoSdf.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));  // 設置東京時區
 
SimpleDateFormat londonSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 倫敦
londonSdf.setTimeZone(TimeZone.getTimeZone("Europe/London"));  // 設置倫敦時區
 
System.out.println("毫秒數:" + date.getTime() + ", 北京時間:" + bjSdf.format(date));
System.out.println("毫秒數:" + date.getTime() + ", 東京時間:" + tokyoSdf.format(date));
System.out.println("毫秒數:" + date.getTime() + ", 倫敦時間:" + londonSdf.format(date));

輸出為:

毫秒數:1503544630000, 北京時間:2017-08-24 11:17:10

毫秒數:1503544630000, 東京時間:2017-08-24 12:17:10

毫秒數:1503544630000, 倫敦時間:2017-08-24 04:17:10


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM