开发者

How to convert a date to UTC

I need to change de format of a date to UTC format.

File fi开发者_运维百科le = new File();

...

file.lastModified();

I need to convert the lastModified date of a file in UTC format.


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); 

Something like that...!!


Quite simply:

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

This works because the long value returned by File.lastModified() represents the number of milliseconds since the epoch (00:00:00 GMT, January 1, 1970) as stated in the Javadoc. And the same is true of java.util.Date. So they are both in UTC/GMT already. When a date is converted to a string such as via Date.toString() or a DateFormat object, it's typically expressed in the local timezone, but the long value it stores is timezone-agnostic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜