java操作時間方式基礎教程demo
更新時間:2023年10月18日 11:18:05 作者:bug生產(chǎn)者
這篇文章主要為大家介紹了java操作時間方式demo基礎教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
java操作時間的方式
獲取年月日時分秒
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小時制的小時 System.out.println("時"+cal.get(Calendar.HOUR_OF_DAY)); // 24小時制的小時 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()); // 阿拉伯數(shù)字 相當于local.getMonth().getValue() System.out.println("日"+local.getDayOfMonth()); System.out.println("時"+local.getHour()); // 24小時制的小時 System.out.println("分"+local.getMinute()); System.out.println("秒"+local.getSecond()); } }
時間格式化中的字符含義
字符 | 描述 |
---|---|
G | 時代指示器(AD) |
y | 年(2001) |
M | 月(07) |
d | 天(20) |
h | 帶有A.M./P.M.的小時(1~12) |
H | 小時(0~23) |
m | 分鐘(0~59) |
s | 秒(0~59) |
S | 毫秒 |
E | 周幾(星期四) |
D | 一年中的第幾天 |
w | 一年中的第幾周 |
W | 一月中的第幾周 |
a | A.M./P.M.標記 |
k | 一天中的第幾個小時(1~24) |
K | 帶有A.M./P.M.的小時 |
z | 時區(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.標記 format = new SimpleDateFormat("yyyy.MM.dd a"); //2021.01.14 下午 System.out.println(format.format(new Date())); // 一天中的第幾個小時(1~24) format = new SimpleDateFormat("yyyy.MM.dd k"); //2021.01.14 14 System.out.println(format.format(new Date())); // 帶有A.M./P.M.的小時 format = new SimpleDateFormat("yyyy.MM.dd K"); //2021.01.14 2 System.out.println(format.format(new Date())); // 時區(qū) format = new SimpleDateFormat("yyyy.MM.dd z"); //2021.01.14 14 System.out.println(format.format(new Date()));
以上就是java操作時間方式的詳細內容,更多關于java操作時間方式的資料請關注腳本之家其它相關文章!
相關文章
Springboot2 session設置超時時間無效的解決
這篇文章主要介紹了Springboot2 session設置超時時間無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Spring MVC 關于controller的字符編碼問題
在使用springMVC框架構建web應用,客戶端常會請求字符串、整型、json等格式的數(shù)據(jù),通常使用@ResponseBody注解使 controller回應相應的數(shù)據(jù)而不是去渲染某個頁面。2017-03-03MybatisPlus 主鍵策略之type=IdType.ASSIGN_ID等詳解
雪花算法(雪花)是微博開源的分布式ID生成算法其核心思想就是:使用一個64位的長型的數(shù)字作為全局唯一ID,這篇文章主要介紹了MybatisPlus 主鍵策略(type=IdType.ASSIGN_ID等詳解),需要的朋友可以參考下2024-04-04