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 zone Calendar cal = Calendar.getInstance(); // Figure out what day of the year today is cal.setTimeInMillis(System.currentTimeMillis()); // Set to the current time int 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 fields int 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 year cal.set(Calendar.MONTH, Calendar.MAY); // Set the month cal.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY); // Set the day of week cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3); // Set the week int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); // Query the day in month // Get a Date object that represents three months from now cal.setTimeInMillis(System.currentTimeMillis()); // Current time cal.add(Calendar.MONTH, 3); // Add 3 months Date expiration = cal.getTime(); // Retrieve result as a Date long millis = cal.getTimeInMillis(); // or get it as a long
2.時間的格式:
// current hours and minutes long now = System.currentTimeMillis(); String s = String.format("%tR", now); // "15:12" // Current month/day/year Date d = new Date(now); s = String.format("%tD", d); // "07/13/04" // Hours and minutes using 12-hour clock Calendar 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 locale DateFormat defaultDate = DateFormat.getDateInstance(); System.out.println(defaultDate.format(new Date())); // Display the current time using a short time format for the current locale DateFormat shortTime = DateFormat.getTimeInstance(DateFormat.SHORT); System.out.println(shortTime.format(new Date())); // Display date and time using a long format for both DateFormat 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 syntax DateFormat 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 */ }