Java常用的時間類以及其轉(zhuǎn)化方式
最近經(jīng)常用到時間這個參數(shù),無論是自己獲取到傳進去,還是從數(shù)據(jù)庫獲取出來,都是Java開發(fā)必備的一門基本功。
一、Date類
類 Date 表示特定的瞬間,精確到毫秒。
最簡單也最經(jīng)典的用法:
Date date = new Date();// 獲取當前的年月日時分秒以及星期和時區(qū)

但是,如果我們按自己想要的格式來顯示日期,一般會引用DateFormat類的子類SimpleDateFormat
二、DateFormat類
DateFormat 是日期/時間格式化子類的抽象類,它以與語言無關(guān)的方式格式化并解析日期或時間。
DateFormat類是抽象類,所以使用其子類SimpleDateFormat。
一般日期類型轉(zhuǎn)String類型,或者自定義String類型轉(zhuǎn)日期類型都會用到它。

注:"HH:mm:ss"和"hh:mm:ss" ,前者是24小時制,后者是12小時制

注:SimpleDateFormat中對應String中的格式,才能成功轉(zhuǎn)換
三、小案例:今天是今年的第幾天?
思路:
將今年第一天和今天都封裝到字符串里
將字符串轉(zhuǎn)換成日期對象
將日期對象轉(zhuǎn)換成毫秒值
讓今天的毫秒值減去第一天的毫秒值
將毫秒值轉(zhuǎn)換成天數(shù)

四、Calendar類
Calendar 類是一個抽象類,它為特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日歷字段之間的轉(zhuǎn)換提供了一些方法,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法。
public static void main(String[] args) {
//Calendar對象,無參,默認為當前日期
Calendar calendar =new GregorianCalendar();
//獲取當前年份
System.out.println(calendar.get(Calendar.YEAR));
//獲取當前月份 0表示一月,1表示二月......11表示12月
System.out.println(calendar.get(Calendar.MONTH));
//獲取當前日期 也可以使用DAY_OF_MONTH
System.out.println(calendar.get(Calendar.DATE));
//獲取當前時 24小時進制
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
//獲取當前分
System.out.println(calendar.get(Calendar.MINUTE));
//獲取當前秒
System.out.println(calendar.get(Calendar.SECOND));
//獲取今天是這個月的第幾個星期
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH));
//獲取今天是星期幾 1表示星期天,2表示星期一......7表率星期六
System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
// 設置日期時間(月份是從0開始的)(法一)
calendar =new GregorianCalendar(2021, 11, 14, 20, 20,20);
// 2021-11-14 20:20:20
//(法二)
calendar.set(Calendar.YEAR, 2021);
calendar.set(Calendar.MONTH, 10);
calendar.set(Calendar.DAY, 14);
System.out.println("Calendar和Date轉(zhuǎn)換************");
Date now = calendar.getTime();
System.out.println(now);
System.out.println(calendar.setTime(now));
System.out.println("Calendar日期計算以及判斷***********");
calendar = new GregorianCalendar();
Calendar calendar2 = new GregorianCalendar();
calendar2.set(Calendar.YEAR, 2035);
//是否在時間(calendar2)之后
System.out.println(calendar.after(calendar2));
//是否在時間(calendar2)之前
System.out.println(calendar.before(calendar2));
//增加多少年月日,時分秒與之同理
calendar.add(Calendar.YEAR, -10);
calendar.add(Calendar.MONTH, 5);
calendar.add(Calendar.DAY, 1);
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合ShardingSphere5.x實現(xiàn)數(shù)據(jù)加解密功能(最新推薦)
這篇文章主要介紹了SpringBoot整合ShardingSphere5.x實現(xiàn)數(shù)據(jù)加解密功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
SpringSecurity中的EnableWebSecurity注解啟用Web安全詳解
這篇文章主要介紹了SpringSecurity中的EnableWebSecurity注解啟用Web安全詳解,@EnableWebSecurity是Spring?Security用于啟用Web安全的注解,典型的用法是該注解用在某個Web安全配置類上,實現(xiàn)了接口,需要的朋友可以參考下2023-12-12
Mybatis plus 配置多數(shù)據(jù)源的實現(xiàn)示例
這篇文章主要介紹了Mybatis plus 配置多數(shù)據(jù)源的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
解決springcloud 配置gateway 出現(xiàn)錯誤的問題
今天給大家分享springcloud 配置gateway 出現(xiàn)錯誤的問題,其實解決方法很簡單,只需要降低springcloud版本,改成Hoxton.SR5就好了,再次改成Hoxton.SR12,也不報錯了,下面給大家展示下,感興趣的朋友一起看看吧2021-11-11
SpringBoot通過計劃任務發(fā)送郵件提醒的代碼詳解
在實際線上項目中,有不斷接受到推送方發(fā)來的數(shù)據(jù)場景,而且是不間斷的發(fā)送,如果忽然間斷了,應該是出問題了,需要及時檢查原因,這種情況比較適合用計劃任務做檢查判斷,出問題發(fā)郵件提醒,本文給大家介紹了SpringBoot通過計劃任務發(fā)送郵件提醒,需要的朋友可以參考下2024-11-11

