java Dates &Times完全总结

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/1000Long.MAX_VALUE/1000 之间的差值。
'Q' 自协调世界时 (UTC) 1970 年 1 月 1 日 00:00:00 至现在所经过的毫秒数,即 Long.MIN_VALUELong.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 */ }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章