如何将日期转换为UTC

我需要将日期格式更改为UTC格式。

文件file = new File();

file.lastModified();

我需要以UTC格式转换文件的lastModified日期。

 String lv_dateFormateInUTC=""; //Will hold the final converted date SimpleDateFormat lv_formatter = new SimpleDateFormat(); lv_formatter.setTimeZone(TimeZone.getTimeZone("UTC")); lv_dateFormateInUTC = lv_formatter.format(lv_localDate); 

像这样…… !!

很简单:

 Date date = new Date(file.lastModified()) 

这是有效的,因为File.lastModified()返回的long值表示自Javadoc中所述的纪元(1970年1月1日00:00:00 GMT)以来的毫秒数。 java.util.Date也是如此。 所以他们已经是UTC / GMT。 当日期转换为字符串(如通过Date.toString()或DateFormat对象)时,它通常以本地时区表示,但它存储的long值与时区无关。