java操作時(shí)間方式基礎(chǔ)教程demo
java操作時(shí)間的方式
獲取年月日時(shí)分秒
public class Test {
public static void main(String[] args) {
System.out.println("----------使用Calendar--------------------");
Calendar cal = Calendar.getInstance();
System.out.println("年"+cal.get(Calendar.YEAR));
System.out.println("月"+(cal.get(Calendar.MONTH)+1)); // Calendar.MONTH 獲取到的是0-11
System.out.println("日"+cal.get(Calendar.DATE));
System.out.println(cal.get(Calendar.HOUR)); // 12小時(shí)制的小時(shí)
System.out.println("時(shí)"+cal.get(Calendar.HOUR_OF_DAY)); // 24小時(shí)制的小時(shí)
System.out.println("分"+cal.get(Calendar.MINUTE));
System.out.println("秒"+cal.get(Calendar.SECOND));
System.out.println("--------------使用java8的LocalDateTime----------------");
LocalDateTime local = LocalDateTime.now();
System.out.println("年"+local.getYear());
System.out.println(local.getMonth().name()); // 英文的月
System.out.println("月"+local.getMonthValue()); // 阿拉伯?dāng)?shù)字 相當(dāng)于local.getMonth().getValue()
System.out.println("日"+local.getDayOfMonth());
System.out.println("時(shí)"+local.getHour()); // 24小時(shí)制的小時(shí)
System.out.println("分"+local.getMinute());
System.out.println("秒"+local.getSecond());
}
}時(shí)間格式化中的字符含義
| 字符 | 描述 |
|---|---|
| G | 時(shí)代指示器(AD) |
| y | 年(2001) |
| M | 月(07) |
| d | 天(20) |
| h | 帶有A.M./P.M.的小時(shí)(1~12) |
| H | 小時(shí)(0~23) |
| m | 分鐘(0~59) |
| s | 秒(0~59) |
| S | 毫秒 |
| E | 周幾(星期四) |
| D | 一年中的第幾天 |
| w | 一年中的第幾周 |
| W | 一月中的第幾周 |
| a | A.M./P.M.標(biāo)記 |
| k | 一天中的第幾個(gè)小時(shí)(1~24) |
| K | 帶有A.M./P.M.的小時(shí) |
| z | 時(shí)區(qū) |
DateFormat format = new SimpleDateFormat("yyyy.MM.dd E"); //2021.01.14 星期四
System.out.println(format.format(new Date()));
// 一年中的第幾天
format = new SimpleDateFormat("yyyy.MM.dd D"); //2021.01.14 14
System.out.println(format.format(new Date()));
// 一年中的第幾周
format = new SimpleDateFormat("yyyy.MM.dd w"); //2021.01.14 3
System.out.println(format.format(new Date()));
// 一月中的第幾周
format = new SimpleDateFormat("yyyy.MM.dd W"); //2021.01.14 3
System.out.println(format.format(new Date()));
// A.M./P.M.標(biāo)記
format = new SimpleDateFormat("yyyy.MM.dd a"); //2021.01.14 下午
System.out.println(format.format(new Date()));
// 一天中的第幾個(gè)小時(shí)(1~24)
format = new SimpleDateFormat("yyyy.MM.dd k"); //2021.01.14 14
System.out.println(format.format(new Date()));
// 帶有A.M./P.M.的小時(shí)
format = new SimpleDateFormat("yyyy.MM.dd K"); //2021.01.14 2
System.out.println(format.format(new Date()));
// 時(shí)區(qū)
format = new SimpleDateFormat("yyyy.MM.dd z"); //2021.01.14 14
System.out.println(format.format(new Date()));以上就是java操作時(shí)間方式的詳細(xì)內(nèi)容,更多關(guān)于java操作時(shí)間方式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
劍指Offer之Java算法習(xí)題精講字符串操作與數(shù)組及二叉搜索樹(shù)
跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03
使用Feign傳遞請(qǐng)求頭信息(Finchley版本)
這篇文章主要介紹了使用Feign傳遞請(qǐng)求頭信息(Finchley版本),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
java拼接字符串時(shí)去掉最后一個(gè)多余逗號(hào)的方法
這篇文章主要介紹了java拼接字符串時(shí)去掉最后一個(gè)多余逗號(hào)的方法,實(shí)例分析了java操作字符串的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
springboot加載命令行參數(shù)ApplicationArguments的實(shí)現(xiàn)
本文主要介紹了springboot加載命令行參數(shù)ApplicationArguments的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
深入解析java HashMap實(shí)現(xiàn)原理
這篇文章主要介紹了深入解析java HashMap實(shí)現(xiàn)原理的相關(guān)資料,需要的朋友可以參考下2015-09-09
SpringBoot中手動(dòng)開(kāi)啟事務(wù)的實(shí)現(xiàn)方法
在Spring Boot中,除了使用@Transactional注解外,還可以通過(guò)TransactionTemplate或PlatformTransactionManager手動(dòng)控制事務(wù),本文給大家介紹SpringBoot中如何手動(dòng)開(kāi)啟事務(wù),感興趣的朋友跟隨小編一起看看吧2025-11-11
logback的DuplicateMessageFilter日志過(guò)濾操作源碼解讀
這篇文章主要為大家介紹了logback的DuplicateMessageFilter日志過(guò)濾操作源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

