『壹』 java問題 獲取當前時間
getDate();
換成
new Date()就可以了,注意這里Date()是java.util包中的。
這樣輸出的時間格式是:
dow mon dd hh:mm:ss zzz yyyy其中:
dow 是一周中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat)。
mon 是月份 (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)。
dd 是一月中的某一天(01 至 31),顯示為兩位十進制數。
hh 是一天中的小時(00 至 23),顯示為兩位十進制數。
mm 是小時中的分鍾(00 至 59),顯示為兩位十進制數。
ss 是分鍾中的秒數(00 至 61),顯示為兩位十進制數。
zzz 是時區(並可以反映夏令時)。標准時區縮寫包括方法 parse 識別的時區縮寫。如果不提供時區信息,則 zzz 為空,即根本不包括任何字元。
yyyy 是年份,顯示為 4 位十進制數。
例如:今天的某個時間 Wed May 12 10:28:34 CST 2010
如果以上面的這種時間形式直接插入到資料庫,可能可讀性較差
因此需要對時間格式進行適當的調整
可利用java提供的java.text.SimpleDateFormat類進行修改
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
輸出的時間格式為 2010-05-12 10:28:34
最後將getDate()修改成
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
就可以了
『貳』 java Date Calendar 區別
Calendar是日歷
Date是時間
Date用來表示當前時間,通常是用來單純的專記錄一個時屬間,它提供的api也非常少
而Calendar則有非常多的api,
例如,
獲取今天是星期幾
今天是哪個月,本月有幾天,今天是本月的第幾個星期
今天是今年的第幾天,等等等等,Date是無法完成的!!
『叄』 java中utc時間怎麼轉換為本地時間
utc毫秒值是個絕對值,和時區無關。如果需要轉換為對應時區的時間表示,可以使用java.text.DateFormat的setTimeZone(timeZone)之後,進行format
『肆』 java中如何對時間做國際化處理啊
二 java中的相關類介紹
<1>java.util.Locale
Locale類的一個實例通常包含國家和語言信息。其中的每一個部分都是由基於國際標准化組織(ISO)制定的國家代碼ISO-3166和語言代碼ISO-639的兩字元的字元串構成的
01.// Get the current system date and time. 02.Date date = new Date(); 03. 04.// Get a France locale using a Locale constant. 05.Locale localeZN = Locale.CHINESE; 06. 07.// Create an English/US locale using the constructor. 08.Locale localeEN = new Locale("en", "US" ); 09. 10.// Get a date time formatter for display in France. 11.DateFormat fullDateFormatFR = 12.DateFormat.getDateTimeInstance( 13.DateFormat.FULL, 14.DateFormat.FULL, 15.localeZN); 16. 17.// Get a date time formatter for display in the U.S. 18.DateFormat fullDateFormatEN = 19.DateFormat.getDateTimeInstance( 20.DateFormat.FULL, 21.DateFormat.FULL, 22.localeEN); 23. 24.System.out.println("Locale: " + localeZN.getDisplayName()); 25.System.out.println(fullDateFormatFR.format(date)); 26.System.out.println("Locale: " + localeEN.getDisplayName()); 27.System.out.println(fullDateFormatEN.format(date)); <2>java.util.TimeZone
TimeZone類的實例包含了一個與格林威治標准時間(GMT)相比較得出的以微秒為單位的時區偏移量,而且它還處理夏令時。getDefault從系統時鍾返回時區數據
01./*TimeZone對象給我們的是原始的偏移量,也就是與GMT相差的微秒數,而且還會告訴我們這個時區是否使用夏令時*/ 02. 03. TimeZone timeZoneFL = TimeZone.getDefault(); 04. System.out.println("\n" + timeZoneFL.getDisplayName()); 05. System.out.println("RawOffset: " + timeZoneFL.getRawOffset()); 06. System.out.println("Uses daylight saving: " + timeZoneFL.useDaylightTime()); 07. 08. TimeZone timeZoneLondon = TimeZone.getTimeZone("America/New_York"); 09. System.out.println("\n" + timeZoneLondon.getDisplayName()); 10. System.out.println("RawOffset: " + timeZoneLondon.getRawOffset()); 11. System.out.println("Uses daylight saving: " + timeZoneLondon.useDaylightTime()); 12. 13. TimeZone timeZoneParis = TimeZone.getTimeZone("Europe/Paris"); 14. System.out.println("\n" + timeZoneParis.getDisplayName()); 15. System.out.println("RawOffset: " + timeZoneParis.getRawOffset()); 16. System.out.println("Uses daylight saving: " + timeZoneParis.useDaylightTime()); 混合起來能夠支持國際化的需求:
01. Locale localeZN = Locale.CHINESE; 02. Locale localeEN = Locale.US; 03. 04. TimeZone timeZoneHZ = TimeZone.getDefault(); 05. TimeZone timeZoneUS = TimeZone.getTimeZone("America/New_York"); 06. 07. 08. DateFormat dateFormatter = DateFormat.getDateTimeInstance( 09. DateFormat.FULL, 10. DateFormat.FULL, 11. localeZN); 12. DateFormat dateFormatterParis = DateFormat.getDateTimeInstance( 13. DateFormat.FULL, 14. DateFormat.FULL, 15. localeEN); 16. 17. Date curDate = new Date(); 18. 19. System.out.println("Display for China HZ."); 20. System.out.println(timeZoneHZ.getDisplayName(localeZN)); 21. dateFormatter.setTimeZone(timeZoneHZ); 22. System.out.println(dateFormatter.format(curDate)); 23. 24. dateFormatter.setTimeZone(timeZoneUS); 25. System.out.println(timeZoneUS.getDisplayName(localeZN)); 26. System.out.println(dateFormatter.format(curDate)); 27. 28. System.out.println("\nDisplay for US office."); 29. System.out.println(timeZoneHZ.getDisplayName(localeEN)); 30. dateFormatterParis.setTimeZone(timeZoneHZ); 31. System.out.println(dateFormatterParis.format(curDate)); 32. 33. dateFormatterParis.setTimeZone(timeZoneUS); 34. System.out.println(timeZoneUS.getDisplayName(localeEN)); 35. System.out.println(dateFormatterParis.format(curDate)); <3>System.getCurrentTime()
返回當前系統的時間與UTC 1970:01:01 00:00:00時間之間的毫秒差距
『伍』 java dataformat 的疑問
你格式化時間的時候錯了~~
你應該去仔細看看api
我先給你一個簡單的示例,你看回看就明白了:答
String time=new SimpleDateFormat("yyyy/MM/dd/hh/mm/ss").format(new Date());