JAVA日期處理類詳解
Date類
java.util.Date類表示特定的瞬間,精確到毫秒需要導(dǎo)包。注意,此時(shí)Date類中多個(gè)包中都存在,不要導(dǎo)錯(cuò)導(dǎo)。
構(gòu)造方法
Date() 分配 Date 對象并初始化此對象,以表示分配它的時(shí)間(精確到毫秒)。 Date(long date) 分配 Date 對象并初始化此對象,以表示自從標(biāo)準(zhǔn)基準(zhǔn)時(shí)間(稱為“歷元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)經(jīng)過date ms后的指定毫秒數(shù)。
簡單來說:使用無參構(gòu)造,可以自動(dòng)設(shè)置當(dāng)前系統(tǒng)時(shí)間的毫秒時(shí)刻;指定long類型的構(gòu)造參數(shù),可以自定義毫秒時(shí)刻。
public class DateDemo { public static void main(String[] args) { //創(chuàng)建日期對象,把當(dāng)前的時(shí)間轉(zhuǎn)成日期對象 System.out.println(new Date()); //創(chuàng)建日期對象,把當(dāng)前的毫秒值轉(zhuǎn)成日期對象 System.out.println(new Date(0L)); //1970年1月1日經(jīng)過100000000L以后的時(shí)間 System.out.println(new Date(100000000L)); } } //結(jié)果 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類中的多數(shù)方法已經(jīng)過時(shí)(被Calender類代替),常用的方法有:
long getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 對象表示的毫秒數(shù)。 void setTime(long time) 設(shè)置此 Date 對象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的時(shí)間點(diǎn)。 String toString() 把此Date對象轉(zhuǎn)換為以下形式的 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后出現(xiàn),替換掉了許多Date的方法。該類將所有可能用到的時(shí)間信息封裝為靜態(tài)成員變量,方便獲取。日歷類就是方便獲取各個(gè)時(shí)間屬性的。
創(chuàng)建Calendar對象
Calendar為抽象類,由于語言敏感性,Calendar類在創(chuàng)建對象時(shí)并非直接創(chuàng)建,而是通過靜態(tài)方法創(chuàng)建,返回子類GregorianCalendar對象,如下:
static Calendar getInstance() 使用默認(rèn)時(shí)區(qū)和語言環(huán)境獲得一個(gè)日歷
所以:
//獲取當(dāng)前的日歷對象 Calendar instance = Calendar.getInstance();
Calendar類中提供很多成員常量(靜態(tài)的,由類名Calendar去調(diào),都是int類型),代表給定的日歷字段:
字段值 | 含義 |
---|---|
YEAR | 年 |
MONTH | 月(從0開始,可以+1使用) |
DAY_OF_MONTH | 月中的天(幾號(hào)) |
HOUR | 時(shí)(12小時(shí)制) |
HOUR_OF_DAY | 時(shí)(24小時(shí)制) |
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); } } //結(jié)果: 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) 將給定的日歷字段設(shè)置為給定值。 //field:域,成員變量
public class DateDemo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); //設(shè)置年為4300年 calendar.set(Calendar.YEAR, 4300); //設(shè)置月為2月 calendar.set(Calendar.MONTH, 2); //設(shè)置日為2日 calendar.set(Calendar.DAY_OF_MONTH, 2); //設(shè)置小時(shí)為2時(shí) calendar.set(Calendar.HOUR_OF_DAY, 2); //設(shè)置分鐘為2分 calendar.set(Calendar.MINUTE, 2); //設(shè)置秒為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); } } //結(jié)果 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) 根據(jù)日歷的規(guī)則,為給定的日歷字段添加或減去指定的時(shí)間量。
public class DateDemo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); //將月份減去5個(gè)月(現(xiàn)在是八月) ~!月是從零開始的 calendar.add(Calendar.MONTH,-5); //將年減少2年(現(xiàn)在是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() 返回一個(gè)表示此 Calendar 時(shí)間值(從歷元至現(xiàn)在的毫秒偏移量)的 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是日期/時(shí)間格式化子類的抽象類,我們通過這個(gè)類可以幫我們完成日期和文本之間的轉(zhuǎn)換,也就是可以在Date對象與String對象之間進(jìn)行來回轉(zhuǎn)換。
格式化:按照指定的格式,從Date對象轉(zhuǎn)換為String對象。
解析:按照指定的格式,從String對象轉(zhuǎn)換為Date對象.
由于DateFormat為抽象類,不能直接使用,所以需要常用的子類java.text.SimpleDateFormat。這個(gè)類需要一個(gè)模式(格式)來指定格式化或解析的標(biāo)準(zhǔn)。
常用構(gòu)造方法
SimpleDateFormat(String pattern) 用給定的模式和默認(rèn)語言環(huán)境的日期格式符號(hào)構(gòu)造 //pattern代表日期時(shí)間的自定義格式 比如:"yyyy-MM-dd HH:mm:ss"; "yyyy/MM/dd HH:mm:ss"; "yyyy年MM月dd日 HH:mm:ss" SimpleDateFormat() 用默認(rèn)的模式和默認(rèn)語言環(huán)境的日期格式符號(hào)構(gòu)造 SimpleDateFormat。
pattern字符串格式規(guī)則
字母 | 日期或時(shí)間元素 | 表示 | 示例 |
---|---|---|---|
y | 年 | Year | 1996; 95 |
M | 年中的月份 | Month | July; 07 |
D | 年中的天數(shù) | Number | 189 |
d | 月份中的天數(shù) | Number | 10 |
m | 小時(shí)中的分鐘數(shù) | Number | 30 |
s | 分鐘中的秒數(shù) | Number | 55 |
S | 毫秒數(shù) | Number | 978 |
a | Am/Pm標(biāo)記 | Text | Pm |
h | am/pm中的小時(shí)數(shù)(1-12) | Number | 12 |
k | 一天中的小時(shí)數(shù)(1-24) | Number | 24 |
E | 星期中的天數(shù) | Text | Tuesday;Tue |
常用方法
public String format(Date date) 將Date對象格式化為字符串。
public class FormatDemo { public static void main(String[] args) { Date now=new Date(); // 指定 格式器化 的時(shí)間模式(是一個(gè)字符串,用特定字符表示不同的時(shí)間信息) // SimpleDateFormat sdf = new SimpleDateFormat(); //將日期轉(zhuǎn)為字符串 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); //將日期轉(zhuǎn)為字符串 String format = sdf.format(now); System.out.println(format); } } //結(jié)果演示 //21-8-19 上午12:15 2021年08月19日 00:14:12 Process finished with exit code 0
public Date parse(String source) 將字符串解析為Date對象。 //!!!source這個(gè)字符串和SimpleFormat(String Pattern)里的Pattern這個(gè)字符串是同一個(gè)!!!!!!否則會(huì)報(bào)錯(cuò)!
public class FormatDemo { public static void main(String[] args) throws ParseException { Date now=new Date(); // 指定 格式器化 的時(shí)間模式(是一個(gè)字符串,用特定字符表示不同的時(shí)間信息) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //將符合時(shí)間模式的日期字符串轉(zhuǎn)為日期對象 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
練習(xí)
計(jì)算出一個(gè)人已經(jīng)出生了多少天
public static void main(String[]args) throws Exception { System.out.println("請輸入出生日期格式Y(jié)YYY-MM-dd"); Scanner scanner =new Scanner(System.in); //獲取出生日期,鍵盤輸入 String birthdayString= scanner.next(); //將字符串日期,轉(zhuǎn)成Date對象 //創(chuàng)建SimpleDateFormat對象,寫日期模式 SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd"); //調(diào)用方法parse,字符串轉(zhuǎn)成日期對象 Date birthdayDate=sdf.parse(birthday String); //獲取今天的日期對象 Date todayDate =new Date(); //將兩個(gè)日期轉(zhuǎn)成毫秒值,Date類的方法getTime long birthdaySecond =birthdayDate.getTime(); long todaySecond =todayDate.getTime(); long secone=todaySecond - birthdaySecond; if(secone<0) { System.out.println("還沒出生呢"); }else{ System.out.println(大概已經(jīng)出生:""+secone/1000/60/60/24+"天"); } scanner.close(); }
總結(jié)
本篇文章就到這里了希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java使用Hutool執(zhí)行日期的加法和減法操作方法
使用Hutool進(jìn)行日期的加法和減法操作,可以使用`DateUtil.offsetXXX()`方法來實(shí)現(xiàn),這些方法會(huì)返回一個(gè)新的日期,而不是在原日期上進(jìn)行修改,本文給大家介紹Java使用Hutool執(zhí)行日期的加法和減法操作方法,感興趣的朋友一起看看吧2023-11-11詳解Eclipse 字體、字號(hào)的設(shè)置、最佳字體推薦
這篇文章主要介紹了Eclipse 字體、字號(hào)的設(shè)置、最佳字體推薦,需要的朋友可以參考下2020-09-09Springboot利于第三方服務(wù)進(jìn)行ip定位獲取省份城市
本文主要介紹了Springboot利于第三方服務(wù)進(jìn)行ip定位獲取省份城市,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Spring Boot 如何使用Liquibase 進(jìn)行數(shù)據(jù)庫遷移(操作方法)
在Spring Boot應(yīng)用程序中使用Liquibase進(jìn)行數(shù)據(jù)庫遷移是一種強(qiáng)大的方式來管理數(shù)據(jù)庫模式的變化,本文重點(diǎn)講解如何在Spring Boot應(yīng)用程序中使用Liquibase進(jìn)行數(shù)據(jù)庫遷移,從而更好地管理數(shù)據(jù)庫模式的變化,感興趣的朋友跟隨小編一起看看吧2023-09-09Java中策略設(shè)計(jì)模式的實(shí)現(xiàn)及應(yīng)用場景
策略設(shè)計(jì)模式是Java中一種常用的設(shè)計(jì)模式,它通過定義一系列算法并將其封裝成獨(dú)立的策略類,從而使得算法可以在不影響客戶端的情況下隨時(shí)切換。策略設(shè)計(jì)模式主要應(yīng)用于系統(tǒng)中存在多種相似的算法、需要靈活調(diào)整算法邏輯或者需要擴(kuò)展新的算法等場景2023-04-04