Java提供了Date、Calendar兩個類用於處理日期、時間。
但Date的大部分構造器、方法已經過時,不在推薦使用,Calendar類又過於復雜,所以Java8推出了一套新的時間日期庫。
1、Date類
Date常用的構造函數:
Date() 生成一個代表當前日期時間的Date對象,相當於Date(System.currentTimeMillis())
Date(long date) 指定時間戳,默認單位ms。距1970.1.1 00:00:00的毫秒數。
Date常用方法:
boolean after(Date date) 判斷是否在該時間日期之后
boolean before(Date date)
long getTime() 獲取時間戳
void setTime() 設置時間戳
少用Date,盡量用Calendar代替。
2、Calendar類
Calendar類常用方法:
Calendar calendar=Calendar.getInstance(); 使用靜態方法獲取實例,默認為當前的時間日期
calendar.get(Calendar.YEAR) 獲取指定字段的值,參數為預定義的常量,返回值均為int。月份比較特殊,0-11,0表示1月。
calendar.set(Calendar.YEAR,2020); 設置指定字段的值
calendar.set(2020,1,1); 同時設置年月日
calendar.set(2020,1,1,1,1,1); 同時設置年月日時分秒
calendar.add(Calendar.YEAR,2); 增加指定字段的值
calendar.add(Calendar.YEAR,-1); 可以為負數
參數、返回值均為int型。
Calendar類示例:
package test; import java.util.Calendar; //需要實現Cloneable接口 public class Test{ public static void main(String[] args) { //使用靜態方法獲取實例,默認為當前的時間日期 Calendar calendar=Calendar.getInstance(); //獲取年份,打YEAR選擇即可 System.out.println(calendar.get(Calendar.YEAR)); //獲取月份,月份從0-11,0表示1月 System.out.println(calendar.get(Calendar.MONTH)); //獲取日 System.out.println(calendar.get(Calendar.DATE)); //獲取時 System.out.println(calendar.get(Calendar.HOUR)); //設置指定字段的值 calendar.set(Calendar.YEAR,2020); //同時設置年月日 calendar.set(2020,1,1); //同時設置年月日時分秒 calendar.set(2020,1,1,1,1,1); //增加指定字段的值,年份+1 calendar.add(Calendar.YEAR,1); //可以為負數,年份-1 calendar.add(Calendar.YEAR,-1); } }
3、Java8新增的時間日期包
LocalDate類:代表當前時區的日期
LocalTime類:代表當前時區的時間
LocalDateTime:代表當前時區的日期時間
以上三個類都提供了靜態方法now()獲取當前的時間/日期/時間日期對象。
示例:
package test; import java.time.LocalDateTime; public class Test{ public static void main(String[] args){ //使用靜態方法now()獲取當前時間日期 LocalDateTime ldt=LocalDateTime.now(); //使用plusXxx(int x)增加指定字段的值 ldt.plusDays(1); //Day字段的值+1 //使用minusXxx(int x)方法減少指定的字段的值 ldt.minusHours(1); //Day字段的值-1 } }
有三種方法可以格式化日期、時間。
1、使用DateFormat類
獲取DateFormat實例:
DateFormat.getDateInstance() 只能格式化日期 2019年5月13日
DateFormat.getTimeInstance() 只能格式化時間 下午10:06:07
DateFormat.getDateTimeInstance() 格式化日期時間 2019年5月13日 下午10:06:07
以上方法均為靜態方法。
以上方法均有2個重載方法:
指定顯示樣式,DateFormat類預定義的常量。
DateFormat.getDateInstance(int style) 指定日期顯示樣式
DateFormat.getTimeInstance(int style) 指定時間顯示樣式
DateFormat.getDateTimeInstance(int style,int style) 第一個參數指定Date顯示樣式,第二個參數指定Time顯示樣式
可用常量:
DateForm.SHORT 2019/5/13 下午10:16
DateForm.MEDIUM 2019年5月13日 下午10:17:06 缺省時默認值就是DateForm.MEDIUM
DateForm.LONG 這個不常用
DateForm.FULL 2019年5月13日星期一 中國標准時間 下午10:18:44
以上三個方法可再加一個參數指定國家:
DateFormat.getDateInstance(int style,Locale.CHINA) 指定日期顯示樣式
DateFormat.getTimeInstance(int style,Locale.CHINA) 指定時間顯示樣式
DateFormat.getDateTimeInstance(int style,int style,Locale.CHINA) 第一個參數指定Date顯示樣式,第二個參數指定Time顯示樣式
也可以使用其他國家。缺省第二個參數時,會使用默認值。第二個參數一般可以缺省。
DateFormat實例常用的方法:
String format(Date date) 將Date對象轉換為格式化的字符串,並返回字符串
1 package test; 2 3 import java.text.DateFormat; 4 import java.text.ParseException; 5 import java.util.Date; 6 7 public class Test{ 8 public static void main(String[] args) throws ParseException { 9 //使用靜態方法獲取實例。只能格式化日期 10 DateFormat df1=DateFormat.getDateInstance(); 11 //只能格式化時間 12 DateFormat df2=DateFormat.getTimeInstance(); 13 //格式化日期時間 14 DateFormat df3=DateFormat.getDateTimeInstance(); 15 //要格式化的Date對象 16 Date date=new Date(); 17 //使用format()格式化Date對象 18 System.out.println(df1.format(date)); 19 System.out.println(df2.format(date)); 20 System.out.println(df3.format(date)); 21 } 22 }
2、使用SimpleDateForm類
SimpleDateForm類是DateForm類的子類。SimpleDateForm比DateForm更簡單,功能更強大。
示例:
1 package test; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 public class Test{ 8 public static void main(String[] args) throws ParseException { 9 //創建SimpleDateFormat對象,指定樣式 2019-05-13 22:39:30 10 SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 11 //2019年的第133天。占位符是特定的 12 SimpleDateFormat sdf2=new SimpleDateFormat("y年的第D天"); 13 //要格式化的Date對象 14 Date date=new Date(); 15 //使用format()方法格式化Date對象為字符串,返回字符串 16 System.out.println(sdf1.format(date)); 17 System.out.println(sdf2.format(date)); 18 } 19 }
占位符:
G "公元"
y 四位數年份
M 月
d 日
h 時 在上午或下午 (1~12)
H 時 在一天中 (0~23)
m 分
s 秒
S 毫秒
E 一周中的周幾
D 一年中的第幾天
w 一年中第幾個星期
a 上午 / 下午 標記符
k 時(1~24)
K 時 在上午或下午 (0~11)
DateFormat只能使用特定的日期時間格式,SimpleDateFormat是自定義日期時間格式。
3、使用Java8新增的DateTimeFormatter類
DateTimeFormatter相當於DateFormat、SimpleDateFormat的合體,可以使用預定義的日期時間格式,也可以使用自定義的格式。但使用方式有些不同。
1 package test; 2 3 import java.text.ParseException; 4 import java.time.LocalDateTime; 5 import java.time.format.DateTimeFormatter; 6 import java.time.format.FormatStyle; 7 8 public class Test{ 9 public static void main(String[] args) throws ParseException { 10 //使用預定義的格式。和DateFormat不同,以下三個方法方法均沒有重載方法,只能這樣用。預定義的常量為FormatStyle類的SHORT、MEDIUM、LONG、FULL 11 DateTimeFormatter df1=DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); //只能格式化日期部分 12 DateTimeFormatter df2=DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT); //只能格式化時間部分 13 DateTimeFormatter df3=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT,FormatStyle.SHORT); //格式化日期時間 14 15 //使用自定義的格式 16 DateTimeFormatter df4=DateTimeFormatter.ofPattern("y-M-d H:m:s"); 17 18 //要格式化的時間日期對象,只能用LocalDateTime。只涉及到日期也可以用LocalDate類,只涉及時間也可以用LocalTime類 19 LocalDateTime ldt=LocalDateTime.now(); 20 21 //格式化,不能用Date類的實例作為參數 22 System.out.println(df1.format(ldt)); 23 System.out.println(df2.format(ldt)); 24 System.out.println(df3.format(ldt)); 25 System.out.println(df4.format(ldt)); 26 } 27 }