如何在Android中使用GMT获取当前日期?

dip*_*ali 0 java android gmt

我希望当前日期在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()));
   }
Run Code Online (Sandbox Code Playgroud)

//调用上面的函数.

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = getGMTDate("yyyy-MM-dd HH:mm:ss");
Run Code Online (Sandbox Code Playgroud)

当我更改设备日期时,时间以GMT格式显示,但日期不显示在GMT时区中.显示设备的日期.但我想要当前的GMT日期.

小智 5

这可能有效

        SimpleDateFormat dateFormatGmt = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss");
        dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
        System.out.println(dateFormatGmt.format(new Date())+"");
Run Code Online (Sandbox Code Playgroud)

指定格式,您将在GMT中获得它!

编辑:你也可以检查这个