java日期Calendar類擷取年月日小時分秒星期

來源:互聯網
上載者:User
package com.ecode.li.date;  import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar;  public class CalendarTest {  /**   * Calendar類使用詳解   *    * @param   */  public static void main(String[] args) {   // 執行個體化   Calendar cale = Calendar.getInstance();   if (cale instanceof GregorianCalendar) {    System.out.println("這說明產生了一個GregorianCalendar類對象,且該類是Calendar抽象類別的子類  ");   }   // 從一個 Calendar 對象中擷取 Date 對象   Date date = cale.getTime();   /*    * 將 Date 對象反應到一個 Calendar 對象中, Calendar/GregorianCalendar 沒有建構函式可以接受    * Date 對象 所以我們必需先獲得一個執行個體,然後設定 Date 對象    */   cale.setTime(date);   // 當前年   int year = cale.get(Calendar.YEAR);   System.out.println(year);    // 當前月 Calendar.MONTH從0開始   int month = (cale.get(Calendar.MONTH)) + 1;   System.out.println(month);    // 當前月的第幾天:即當前日   int day_of_month = cale.get(Calendar.DAY_OF_MONTH);     // Calendar.DAY_OF_MONTH 和 Calendar.DATE 是等價的   int dates = cale.get(Calendar.DATE);   System.out.println(dates);   System.out.println(day_of_month);    // 當前時:HOUR_OF_DAY-24小時制   int hour24 = cale.get(Calendar.HOUR_OF_DAY);   // HOUR-12小時制   int hour12 = cale.get(Calendar.HOUR);   System.out.println(hour24);   System.out.println(hour12);    // 當前分   int minute = cale.get(Calendar.MINUTE);   // 當前秒   int second = cale.get(Calendar.SECOND);   System.out.println(minute);   System.out.println(second);    // 星期幾 Calendar.DAY_OF_WEEK用數字(1~7)表示(星期日~星期六)     int day_of_week = cale.get(Calendar.DAY_OF_WEEK) - 1;   System.out.println(day_of_week);    // 當前年的第幾周   int week_of_year = cale.get(Calendar.WEEK_OF_YEAR);   System.out.println(week_of_year);    // //當前月的星期數   // int week_of_month = cale.get(Calendar.WEEK_OF_MONTH);   //        // //當前月中的第幾個星期   // int day_of_week_in_month = cale.get(Calendar.DAY_OF_WEEK_IN_MONTH);   //        // //當前年的第幾天   // int day_of_year = cale.get(Calendar.DAY_OF_YEAR);  } } 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.