Java的日期類和日期格式化類

來源:互聯網
上載者:User

標籤:get   tin   sys   格式化   系統時間   預設   一個   birt   cond   

日期類:

 1 Date date = new Date(); // 擷取當前的系統時間 2 System.out.println("年份:"+ date.getYear()); 3          4 Calendar calendar = Calendar.getInstance(); //擷取當前的系統時間。 5 System.out.println("年:"+ calendar.get(Calendar.YEAR)); 6 System.out.println("月:"+ (calendar.get(Calendar.MONTH)+1)); 7 System.out.println("日:"+ calendar.get(Calendar.DATE)); 8          9 System.out.println("時:"+ calendar.get(Calendar.HOUR_OF_DAY));10 System.out.println("分:"+ calendar.get(Calendar.MINUTE));11 System.out.println("秒:"+ calendar.get(Calendar.SECOND));            

日期格式化類:

 1 /* 日期格式化類 SimpleDateFormat  2 * 作用1: 可以把日期轉換轉指定格式的字串 format() 3 * 作用2: 可以把一個 字元轉換成對應的日期。 parse()  4 */ 5 Date date = new Date(); //擷取當前的系統時間。 6 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss") ; //使用了預設的格式建立了一個日期格式化對象。 7 String time = dateFormat.format(date); //可以把日期轉換轉指定格式的字串 8 System.out.println("當前的系統時間:"+ time); 9 10 String birthday = "2000年12月26日 11:29:08";11 Date date2 = dateFormat.parse(birthday); //注意:指定的字串格式必須要與SimpleDateFormat的模式要一致。12 System.out.println(date2);

 

Java的日期類和日期格式化類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.