日期處理類
即使再小的帆也能遠航~
一.目錄
Date類
Calendar類
DataFormat類
二.內容
Date類
java.util.Date類表示特定的瞬間,精確到毫秒需要導包,注意,此時Date類中多個包中都存在,不要導錯導,
構造方法
Date()
分配 Date 物件并初始化此物件,以表示分配它的時間(精確到毫秒),
Date(long date)
分配 Date 物件并初始化此物件,以表示自從標準基準時間(稱為“歷元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)經過date ms后的指定毫秒數,
簡單來說:使用無參構造,可以自動設定當前系統時間的毫秒時刻;指定long型別的構造引數,可以自定義毫秒時刻,
public class DateDemo {
public static void main(String[] args) {
//創建日期物件,把當前的時間轉成日期物件
System.out.println(new Date());
//創建日期物件,把當前的毫秒值轉成日期物件
System.out.println(new Date(0L));
//1970年1月1日經過100000000L以后的時間
System.out.println(new Date(100000000L));
}
}
//結果
18 15:56:43 CST 2021
Thu Jan 01 08:00:00 CST 1970
Fri Jan 02 11:46:40 CST 1970
Process finished with exit code 0
常用方法
Date類中的多數方法已經過時(被Calender類代替),常用的方法有:
long getTime()
回傳自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 物件表示的毫秒數,
void setTime(long time)
設定此 Date 物件,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的時間點,
String toString()
把此Date物件轉換為以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中: dow 是一周中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat),
public static void main (String[]args) throws Exception {
Date date =new Date();
long time =date.getTime();
System.out.println(time);//1594016487419
}
Calendar類
java.util.Calendar是日歷類,在Date后出現,替換掉了許多Date的方法,該類將所有可能用到的時間資訊封裝為靜態成員變數,方便獲取,日歷類就是方便獲取各個時間屬性的,
創建Calendar物件
Calendar為抽象類,由于語言敏感性,Calendar類在創建物件時并非直接創建,而是通過靜態方法創建,回傳子類GregorianCalendar物件,如下:
static Calendar getInstance()
使用默認時區和語言環境獲得一個日歷
所以:
//獲取當前的日歷物件
Calendar instance = Calendar.getInstance();
Calendar類中提供很多成員常量(靜態的,由類名Calendar去調,都是int型別),代表給定的日歷欄位:
| 欄位值 | 含義 |
|---|---|
| YEAR | 年 |
| MONTH | 月(從0開始,可以+1使用) |
| DAY_OF_MONTH | 月中的天(幾號) |
| HOUR | 時(12小時制) |
| HOUR_OF_DAY | 時(24小時制) |
| MINUTE | 分 |
| SECOND | 秒 |
| DAY_OF_WEAK | 周中的天(周幾,周日為1,可以+1使用) |
注意:1、西方星期的開始為周日,中國為周一,2、在Calendar類中,月份的表示是以0-11代表1-12月,
常用方法
-
int get(int field) 回傳給定日歷欄位的值,//演示: public class DateDemo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println(calendar); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); System.out.println(year+"年"+month+"月"+day+"日\t"+hour+":"+minute+":"+second); } } //結果: java.util.GregorianCalendar[time=1629281702695,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2021,MONTH=7,WEEK_OF_YEAR=34,WEEK_OF_MONTH=3,DAY_OF_MONTH=18,DAY_OF_YEAR=230,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=6,HOUR_OF_DAY=18,MINUTE=15,SECOND=2,MILLISECOND=695,ZONE_OFFSET=28800000,DST_OFFSET=0] 2021年7月18日 18:15:2 Process finished with exit code 0 -
void set(int field, int value) 將給定的日歷欄位設定為給定值, //field:域,成員變數public class DateDemo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); //設定年為4300年 calendar.set(Calendar.YEAR, 4300); //設定月為2月 calendar.set(Calendar.MONTH, 2); //設定日為2日 calendar.set(Calendar.DAY_OF_MONTH, 2); //設定小時為2時 calendar.set(Calendar.HOUR_OF_DAY, 2); //設定分鐘為2分 calendar.set(Calendar.MINUTE, 2); //設定秒為2秒 calendar.set(Calendar.SECOND, 2); System.out.println(calendar); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); System.out.println(year+"年"+month+"月"+day+"日\t"+hour+":"+minute+":"+second); } } //結果 java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=4300,MONTH=2,WEEK_OF_YEAR=34,WEEK_OF_MONTH=3,DAY_OF_MONTH=2,DAY_OF_YEAR=230,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=6,HOUR_OF_DAY=2,MINUTE=2,SECOND=2,MILLISECOND=751,ZONE_OFFSET=28800000,DST_OFFSET=0] 4300年2月2日 2:2:2 Process finished with exit code 0 -
abstract void add(int field, int amount) 根據日歷的規則,為給定的日歷欄位添加或減去指定的時間量,public class DateDemo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); //將月份減去5個月(現在是八月) ~!月是從零開始的 calendar.add(Calendar.MONTH,-5); //將年減少2年(現在是2021年) calendar.add(Calendar.YEAR,-2); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); System.out.println("年:"+year); System.out.println("月:"+month); } } 年:2019 月:2 //月是從零開始的 Process finished with exit code 0 -
Date getTime() 回傳一個表示此 Calendar 時間值(從歷元至現在的毫秒偏移量)的 Date 物件,public class DateDemo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); System.out.println(date); } } Wed Aug 18 23:21:51 CST 2021 Process finished with exit code 0
DataFormat類
java.text.DateFormat是日期/時間格式化子類的抽象類,我們通過這個類可以幫我們完成日期和文本之間的轉換,也就是可以在Date物件與String物件之間進行來回轉換,
格式化:按照指定的格式,從Date物件轉換為String物件,
決議:按照指定的格式,從String物件轉換為Date物件.
由于DateFormat為抽象類,不能直接使用,所以需要常用的子類java.text.SimpleDateFormat,這個類需要一個模式(格式)來指定格式化或決議的標準,
常用構造方法
SimpleDateFormat(String pattern)
用給定的模式和默認語言環境的日期格式符號構造
//pattern代表日期時間的自定義格式 比如:"yyyy-MM-dd HH:mm:ss"; "yyyy/MM/dd HH:mm:ss"; "yyyy年MM月dd日 HH:mm:ss"
SimpleDateFormat()
用默認的模式和默認語言環境的日期格式符號構造 SimpleDateFormat,
pattern字串格式規則
| 字母 | 日期或時間元素 | 表示 | 示例 |
|---|---|---|---|
| y | 年 | Year | 1996; 95 |
| M | 年中的月份 | Month | July; 07 |
| D | 年中的天數 | Number | 189 |
| d | 月份中的天數 | Number | 10 |
| m | 小時中的分鐘數 | Number | 30 |
| s | 分鐘中的秒數 | Number | 55 |
| S | 毫秒數 | Number | 978 |
| a | Am/Pm標記 | Text | Pm |
| h | am/pm中的小時數(1-12) | Number | 12 |
| k | 一天中的小時數(1-24) | Number | 24 |
| E | 星期中的天數 | Text | Tuesday;Tue |
常用方法
-
public String format(Date date) 將Date物件格式化為字串,public class FormatDemo { public static void main(String[] args) { Date now=new Date(); // 指定 格式器化 的時間模式(是一個字串,用特定字符表示不同的時間資訊) // SimpleDateFormat sdf = new SimpleDateFormat(); //將日期轉為字串 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); //將日期轉為字串 String format = sdf.format(now); System.out.println(format); } } //結果演示 //21-8-19 上午12:15 2021年08月19日 00:14:12 Process finished with exit code 0 -
public Date parse(String source) 將字串決議為Date物件, //!!!source這個字串和SimpleFormat(String Pattern)里的Pattern這個字串是同一個!!!!!!否則會報錯!public class FormatDemo { public static void main(String[] args) throws ParseException { Date now=new Date(); // 指定 格式器化 的時間模式(是一個字串,用特定字符表示不同的時間資訊) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //將符合時間模式的日期字串轉為日期物件 String timeStr="2021-05-21 13:14:00"; Date parse = sdf.parse(timeStr); System.out.println(parse); } } //演示 Fri May 21 13:14:00 CST 2021 Process finished with exit code 0
練習
計算出一個人已經出生了多少天
public static void main(String[]args) throws Exception {
System.out.println("請輸入出生日期格式YYYY-MM-dd");
Scanner scanner =new Scanner(System.in);
//獲取出生日期,鍵盤輸入
String birthdayString= scanner.next();
//將字串日期,轉成Date物件
//創建SimpleDateFormat物件,寫日期模式
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
//呼叫方法parse,字串轉成日期物件
Date birthdayDate=sdf.parse(birthday String);
//獲取今天的日期物件
Date todayDate =new Date();
//將兩個日期轉成毫秒值,Date類的方法getTime
long birthdaySecond =birthdayDate.getTime();
long todaySecond =todayDate.getTime();
long secone=todaySecond - birthdaySecond;
if(secone<0) {
System.out.println("還沒出生呢");
}else{
System.out.println(大概已經出生:""+secone/1000/60/60/24+"天");
}
scanner.close();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/295154.html
標籤:java
