[英]How to convert a date to 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
值與時區無關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.