方法一:
Date date = new Date();
date.toGMTString(); // jdk高版本中,已经过时,不推荐。
方法二:
DateFormat cstFormat = new SimpleDateFormat();
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
TimeZone cstTime = TimeZone.getTimeZone("CST");
cstFormat.setTimeZone(gmtTime);
gmtFormat.setTimeZone(cstTime);
System.out.println("GMT Time: " + cstFormat.format(date));
System.out.println("CST Time: " + gmtFormat.format(date));
方法三:
public Date getCST(String strGMT) throws ParseException {
DateFormat df = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH);
return df.parse(strGMT);
}
public String getGMT(Date dateCST) {
DateFormat df = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("GMT")); // modify Time Zone.
return(df.format(dateCST));
}
Date date = new Date();
date.toGMTString(); // jdk高版本中,已经过时,不推荐。
方法二:
DateFormat cstFormat = new SimpleDateFormat();
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
TimeZone cstTime = TimeZone.getTimeZone("CST");
cstFormat.setTimeZone(gmtTime);
gmtFormat.setTimeZone(cstTime);
System.out.println("GMT Time: " + cstFormat.format(date));
System.out.println("CST Time: " + gmtFormat.format(date));
方法三:
public Date getCST(String strGMT) throws ParseException {
DateFormat df = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH);
return df.parse(strGMT);
}
public String getGMT(Date dateCST) {
DateFormat df = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("GMT")); // modify Time Zone.
return(df.format(dateCST));
}