格林威治時間(GMT)是指格林威治所在地的標準時間 ,是北京時間減去8個小時後的時間。
1、以前是通過new Date().toGMTString()來獲取,但是已過時;
2、現在通過以下方法獲取:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.Date;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String gmt = dateFormat.format(new Date()) + "Z";
System.out.println("格林威治時間爲:" + gmt);
注意:以上方法如果環境是CST時間則取的是GMT時間,如果本身環境就是GMT時間則取的也是GMT時間。