時間字串和Date之前的轉換
阿新 • • 發佈:2020-12-28
技術標籤:Java進階
1,字串時間轉換Date
使用者傳進來的格式是“20201010”、“2020-1010”或者“2020.10.10”,要轉換為Date都是用Hutool中的DateUtil.parse()方式,作用獲取到的date格式是2020-10-10型別。這樣就不需要判斷使用者輸入的是什麼型別的時間字串。
String strTime = "2020.10.10";
Date date = DateUtil.parse(strTime);
輸出:
2,Date型別轉換為字串
使用DateUtil工具類:
Date date = new Date ();
System.out.println("年月日" + DateUtil.formatDate(date));
System.out.println("時分秒" + DateUtil.formatTime(date));
System.out.println("年月日 時分秒" + DateUtil.formatDateTime(date));
輸出: