本文介紹了Java Calendar:獲取時區時間的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
以下作品(顯示UTC時間)
The following works (shows UTC time)
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
System.out.println(new Date());
但這不(顯示當地時間)
but this doesn't (shows local time)
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
System.out.println(cal.getTime());
System.out.println(new Date());
有什么簡單的東西我錯過了嗎?
Is there something simple, that I'm missing?
推薦答案
您正在打印 Date.toString()
,總是使用默認時區.
You're printing out the result of Date.toString()
, which always uses the default time zone.
我建議你使用 DateFormat
相反,它更適合格式化日期.Date.toString
實際上只適用于調試 - 它無法控制格式.
I suggest you use DateFormat
instead, which is better suited for formatting dates. Date.toString
is really only suitable for debugging - it provides no control over the format.
或者,使用 Joda Time 進行所有日期和時間操作 - 這是一個更好的 API 開始:)
Alternatively, use Joda Time for all your date and time operations - it's a much better API to start with :)
這篇關于Java Calendar:獲取時區時間的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!