java Dates &Times完全總結

來源:互聯網
上載者:User
1.獲得目前時間的方法:
利用java.until.Date
System.err.print("Formant 1: ");
  System.out.println(new Date(System.currentTimeMillis()) );
  //System.out.print("The 2 formant: ");
  
  
  System.err.print("Formant 2: ");
  Date d=new Date();
  long t=d.getTime();
  System.out.println(new Date(t));
  
  //System.out.println(d.toLocaleString());  
  
  System.err.print("Formant 3:");
  Calendar date=Calendar.getInstance();
  date.add(Calendar.DATE, 0);//當前的前一天
  System.out.println(date.getTime());
利用java.until.Calendar
// Get a Calendar for current locale and time zoneCalendar cal = Calendar.getInstance();  // Figure out what day of the year today iscal.setTimeInMillis(System.currentTimeMillis()); // Set to the current timeint dayOfYear = cal.get(Calendar.DAY_OF_YEAR);  // What day of the year is it?// What day of the week does the leap day in the year 2008 occur on?cal.set(2008, Calendar.FEBRUARY, 29);            // Set year, month, day fieldsint dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);   // Query a different field// What day of the month is the 3rd Thursday of May, 2005?cal.set(Calendar.YEAR, 2005);                    // Set the yearcal.set(Calendar.MONTH, Calendar.MAY);           // Set the monthcal.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY); // Set the day of weekcal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3);       // Set the weekint dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); // Query the day in month// Get a Date object that represents three months from nowcal.setTimeInMillis(System.currentTimeMillis()); // Current timecal.add(Calendar.MONTH, 3);                      // Add 3 monthsDate expiration = cal.getTime();                 // Retrieve result as a Datelong millis = cal.getTimeInMillis();             // or get it as a long
 
2.時間的格式:
// current hours and minuteslong now = System.currentTimeMillis();String s = String.format("%tR", now);         // "15:12"// Current month/day/yearDate d = new Date(now);s = String.format("%tD", d);                  // "07/13/04"// Hours and minutes using 12-hour clockCalendar c = Calendar.getInstance();c.setTime(d);s = String.format("%tl:%tM %tp", now, d, c);  // "3:12 pm"
 
時間格式的轉換
日期/時間轉換 

以下日期和時間轉換的尾碼字元是為 't' 和 'T' 轉換定義的。這些類型相似於但不完全等同於那些由 GNU date 和 POSIX strftime(3c) 定義的類型。提供其他轉換類型是為了訪問特定於 Java 的功能(如將 'L' 用作秒中的毫秒)。

以下轉換字元用來格式化時間:

'H' 24 小時制的小時,被格式化為必要時帶前置字元為零的兩位元,即 00 - 23。
'I' 12 小時制的小時,被格式化為必要時帶前置字元為零的兩位元,即 01 - 12。
'k' 24 小時制的小時,即 0 - 23。
'l' 12 小時制的小時,即 1 - 12。
'M' 小時中的分鐘,被格式化為必要時帶前置字元為零的兩位元,即 00 - 59。
'S' 分鐘中的秒,被格式化為必要時帶前置字元為零的兩位元,即 00 - 60 ("60" 是支援閏秒所需的一個特殊值)。
'L' 秒中的毫秒,被格式化為必要時帶前置字元為零的三位元,即 000 - 999。
'N' 秒中的毫微秒,被格式化為必要時帶前置字元為零的九位元,即 000000000 - 999999999。
'p' 特定於語言環境的 上午或下午 標記以小寫形式表示,例如 "am" 或 "pm"。使用轉換首碼 'T' 可以強行將此輸出轉換為大寫形式。
'z' 相對於 GMT 的 RFC 822 格式的數字時區位移量,例如 -0800。
'Z' 表示時區縮寫形式的字串。Formatter 的語言環境將取代參數的語言環境(如果有)。
's' 自國際標準時間 (UTC) 1970 年 1 月 1 日 00:00:00 至現在所經過的秒數,即 Long.MIN_VALUE/1000 與 Long.MAX_VALUE/1000 之間的差值。
'Q' 自國際標準時間 (UTC) 1970 年 1 月 1 日 00:00:00 至現在所經過的毫秒數,即 Long.MIN_VALUE 與 Long.MAX_VALUE 之間的差值。

以下轉換字元用來格式化日期:

'B' 特定於語言環境的月份全稱,例如 "January" 和 "February"。
'b' 特定於語言環境的月份簡稱,例如 "Jan" 和 "Feb"。
'h' 與 'b' 相同。
'A' 特定於語言環境的星期幾全稱,例如 "Sunday" 和 "Monday"
'a' 特定於語言環境的星期幾簡稱,例如 "Sun" 和 "Mon"
'C' 除以 100 的四位元表示的年份,被格式化為必要時帶前置字元為零的兩位元,即 00 - 99
'Y' 年份,被格式化為必要時帶前置字元為零的四位元(至少),例如,0092 等于格裡高利曆的 92 CE。
'y' 年份的最後兩位元,被格式化為必要時帶前置字元為零的兩位元,即 00 - 99。
'j' 一年中的天數,被格式化為必要時帶前置字元為零的三位元,例如,對于格裡高利曆是 001 - 366。
'm' 月份,被格式化為必要時帶前置字元為零的兩位元,即 01 - 13。
'd' 一個月中的天數,被格式化為必要時帶前置字元為零兩位元,即 01 - 31
'e' 一個月中的天數,被格式化為兩位元,即 1 - 31。

以下轉換字元用于格式化常見的日期/時間組合。

'R' 24 小時制的時間,被格式化為 "%tH:%tM"
'T' 24 小時制的時間,被格式化為 "%tH:%tM:%tS"。
'r' 12 小時制的時間,被格式化為 "%tI:%tM:%tS %Tp"。上午或下午標記 ('%Tp') 的位置可能與語言環境有關。
'D' 日期,被格式化為 "%tm/%td/%ty"。
'F' ISO 8601 格式的完整日期,被格式化為 "%tY-%tm-%td"。
'c' 日期和時間,被格式化為 "%ta %tb %td %tT %tZ %tY",例如 "Sun Jul 20 16:17:00 EDT 1969"。

 3.使用DateFormat

import java.util.Date;import java.text.*;// Display today's date using a default format for the current localeDateFormat defaultDate = DateFormat.getDateInstance();System.out.println(defaultDate.format(new Date()));// Display the current time using a short time format for the current localeDateFormat shortTime = DateFormat.getTimeInstance(DateFormat.SHORT);System.out.println(shortTime.format(new Date()));// Display date and time using a long format for bothDateFormat longTimestamp =   DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);System.out.println(longTimestamp.format(new Date()));// Use SimpleDateFormat to define your own formatting template// See java.text.SimpleDateFormat for the template syntaxDateFormat myformat = new SimpleDateFormat("yyyy.MM.dd");  System.out.println(myformat.format(new Date()));try {   // DateFormat can parse dates too  Date leapday = myformat.parse("2000.02.29"); }catch (ParseException e) { /* Handle parsing exception */ }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.