java操作時(shí)間方式基礎(chǔ)教程demo
更新時(shí)間:2023年10月18日 11:18:05 作者:bug生產(chǎn)者
這篇文章主要為大家介紹了java操作時(shí)間方式demo基礎(chǔ)教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
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)文章
java實(shí)現(xiàn)簡(jiǎn)易五子棋游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)易五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06Springboot2 session設(shè)置超時(shí)時(shí)間無(wú)效的解決
這篇文章主要介紹了Springboot2 session設(shè)置超時(shí)時(shí)間無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Spring MVC 關(guān)于controller的字符編碼問(wèn)題
在使用springMVC框架構(gòu)建web應(yīng)用,客戶端常會(huì)請(qǐng)求字符串、整型、json等格式的數(shù)據(jù),通常使用@ResponseBody注解使 controller回應(yīng)相應(yīng)的數(shù)據(jù)而不是去渲染某個(gè)頁(yè)面。2017-03-03MybatisPlus 主鍵策略之type=IdType.ASSIGN_ID等詳解
雪花算法(雪花)是微博開(kāi)源的分布式ID生成算法其核心思想就是:使用一個(gè)64位的長(zhǎng)型的數(shù)字作為全局唯一ID,這篇文章主要介紹了MybatisPlus 主鍵策略(type=IdType.ASSIGN_ID等詳解),需要的朋友可以參考下2024-04-04Java實(shí)現(xiàn)駝峰、下劃線互轉(zhuǎn)的方法
這篇文章主要介紹了Java實(shí)現(xiàn)駝峰、下劃線互轉(zhuǎn)的示例代碼,主要有使用 Guava 實(shí)現(xiàn)和自定義代碼轉(zhuǎn),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05SpringBoot常見(jiàn)問(wèn)題小結(jié)
這篇文章主要介紹了SpringBoot常見(jiàn)問(wèn)題小結(jié),需要的朋友可以參考下2017-07-07