我想要 GMT 时区的当前日期。我使用了以下代码。
public static Date getGMTDate(String dateFormat) throws ParseException
{
SimpleDateFormat dateFormatGmt = new SimpleDateFormat(dateFormat);
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
// Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat(dateFormat);
// Time in GMT
return dateFormatLocal.parse(dateFormatGmt.format(new Date()));
}
//调用上面的函数。
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = getGMTDate("yyyy-MM-dd HH:mm:ss");
当我更改设备日期时,时间将以 GMT 格式显示,但日期不会以 GMT 时区显示。它显示设备的日期。 但我想要当前的 GMT 日期。
最佳答案
这可能有效
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(dateFormatGmt.format(new Date())+"");
指定格式,您将得到 GMT 格式的结果!
编辑:您还可以检查 This
关于java - 如何在android中使用GMT获取当前日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17719529/