Java Calendar使用總結


  • Java Calendar 類時間操作,這也許是創建和管理日歷最簡單的一個方案,示范代碼很簡單。

    演示了獲取時間,日期時間的累加和累減,以及比較。


    原文地址:blog.csdn.NET/joyous/article/details/9630893


    注意事項:

    Calendar 的 month 從 0 開始,也就是全年 12 個月由 0 ~ 11 進行表示。

    而 Calendar.DAY_OF_WEEK 定義和值如下:

    Calendar.SUNDAY = 1
    Calendar.MONDAY = 2
    Calendar.TUESDAY = 3
    Calendar.WEDNESDAY = 4
    Calendar.THURSDAY = 5
    Calendar.FRIDAY = 6
    Calendar.SATURDAY = 7


    SimpleDateFormat 的格式定義

    Letter Date or Time Component Presentation Examples
    G Era designator Text AD
    y Year Year 199696
    Y Week year Year 200909
    M Month in year (context sensitive) Month JulyJul07
    L Month in year (standalone form) Month JulyJul07
    w Week in year Number 27
    W Week in month Number 2
    D Day in year Number 189
    d Day in month Number 10
    F Day of week in month Number 2
    E Day name in week Text TuesdayTue
    u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
    a Am/pm marker Text PM
    H Hour in day (0-23) Number 0
    k Hour in day (1-24) Number 24
    K Hour in am/pm (0-11) Number 0
    h Hour in am/pm (1-12) Number 12
    m Minute in hour Number 30
    s Second in minute Number 55
    S Millisecond Number 978
    z Time zone General time zone Pacific Standard TimePSTGMT-08:00
    Z Time zone RFC 822 time zone -0800
    X Time zone ISO 8601 time zone -08-0800-08:00


    Java Calendar 演示代碼如下:

    [java]  view plain  copy
    1. package demo;  
    2.   
    3. import java.util.Date;  
    4. import java.text.SimpleDateFormat;  
    5. import java.text.DateFormat;  
    6. import java.text.ParseException;  
    7. import java.util.Calendar;  
    8.   
    9. public class Test  
    10. {  
    11.   public Test()  
    12.   {  
    13.   }  
    14.   
    15.   public static void main(String[] args)  
    16.   {  
    17.     // 字符串轉換日期格式  
    18.     // DateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    19.     // 接收傳入參數  
    20.     // String strDate = args[1];  
    21.     // 得到日期格式對象  
    22.     // Date date = fmtDateTime.parse(strDate);  
    23.   
    24.     // 完整顯示今天日期時間  
    25.     String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date());  
    26.     System.out.println(str);  
    27.   
    28.     // 創建 Calendar 對象  
    29.     Calendar calendar = Calendar.getInstance();  
    30.   
    31.     try  
    32.     {  
    33.       // 對 calendar 設置時間的方法  
    34.       // 設置傳入的時間格式  
    35.       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d H:m:s");  
    36.       // 指定一個日期  
    37.       Date date = dateFormat.parse("2013-6-1 13:24:16");  
    38.       // 對 calendar 設置為 date 所定的日期  
    39.       calendar.setTime(date);  
    40.   
    41.       // 按特定格式顯示剛設置的時間  
    42.       str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());  
    43.       System.out.println(str);  
    44.     }  
    45.     catch (ParseException e)  
    46.     {  
    47.       e.printStackTrace();  
    48.     }  
    49.   
    50.     // 或者另一種設置 calendar 方式  
    51.     // 分別爲 year, month, date, hourOfDay, minute, second  
    52.     calendar = Calendar.getInstance();  
    53.     calendar.set(201312173544);  
    54.     str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());  
    55.     System.out.println(str);  
    56.   
    57.     // Calendar 取得當前時間的方法  
    58.     // 初始化 (重置) Calendar 對象  
    59.     calendar = Calendar.getInstance();  
    60.     // 或者用 Date 來初始化 Calendar 對象  
    61.     calendar.setTime(new Date());  
    62.   
    63.     // setTime 類似上面一行  
    64.     // Date date = new Date();  
    65.     // calendar.setTime(date);  
    66.   
    67.     str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());  
    68.     System.out.println(str);  
    69.   
    70.     // 顯示年份  
    71.     int year = calendar.get(Calendar.YEAR);  
    72.     System.out.println("year is = " + String.valueOf(year));  
    73.   
    74.     // 顯示月份 (從0開始, 實際顯示要加一)  
    75.     int month = calendar.get(Calendar.MONTH);  
    76.     System.out.println("nth is = " + (month + 1));  
    77.   
    78.     // 本周幾  
    79.     int week = calendar.get(Calendar.DAY_OF_WEEK);  
    80.     System.out.println("week is = " + week);  
    81.   
    82.     // 今年的第 N 天  
    83.     int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);  
    84.     System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR);  
    85.   
    86.     // 本月第 N 天  
    87.     int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);  
    88.     System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH));  
    89.   
    90.     // 3小時以后  
    91.     calendar.add(Calendar.HOUR_OF_DAY, 3);  
    92.     int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);  
    93.     System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY);  
    94.   
    95.     // 當前分鍾數  
    96.     int MINUTE = calendar.get(Calendar.MINUTE);  
    97.     System.out.println("MINUTE = " + MINUTE);  
    98.   
    99.     // 15 分鍾以后  
    100.     calendar.add(Calendar.MINUTE, 15);  
    101.     MINUTE = calendar.get(Calendar.MINUTE);  
    102.     System.out.println("MINUTE + 15 = " + MINUTE);  
    103.   
    104.     // 30分鍾前  
    105.     calendar.add(Calendar.MINUTE, -30);  
    106.     MINUTE = calendar.get(Calendar.MINUTE);  
    107.     System.out.println("MINUTE - 30 = " + MINUTE);  
    108.   
    109.     // 格式化顯示  
    110.     str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());  
    111.     System.out.println(str);  
    112.   
    113.     // 重置 Calendar 顯示當前時間  
    114.     calendar.setTime(new Date());  
    115.     str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());  
    116.     System.out.println(str);  
    117.   
    118.     // 創建一個 Calendar 用於比較時間  
    119.     Calendar calendarNew = Calendar.getInstance();  
    120.   
    121.     // 設定為 5 小時以前,后者大,顯示 -1  
    122.     calendarNew.add(Calendar.HOUR, -5);  
    123.     System.out.println("時間比較:" + calendarNew.compareTo(calendar));  
    124.   
    125.     // 設定7小時以后,前者大,顯示 1  
    126.     calendarNew.add(Calendar.HOUR, +7);  
    127.     System.out.println("時間比較:" + calendarNew.compareTo(calendar));  
    128.   
    129.     // 退回 2 小時,時間相同,顯示 0  
    130.     calendarNew.add(Calendar.HOUR, -2);  
    131.     System.out.println("時間比較:" + calendarNew.compareTo(calendar));  
    132.   }  
    133. }  
      
  • SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");  
  • /*初始化*/  
  • Calendar cal = Calendar.getInstance();  
  •   
  • int year = cal.get(Calendar.YEAR);//年    
  • int month = cal.get(Calendar.MONTH)+1;//月  
  • int day = cal.get(Calendar.DATE);//日    
  • int hour = cal.get(Calendar.HOUR_OF_DAY);//時    
  • int minute = cal.get(Calendar.MINUTE);//分  
  • int second = cal.get(Calendar.SECOND);//秒    
  • int weekday = cal.get(Calendar.DAY_OF_WEEK);//星期   周日-周六對應1-7  
  • int last = cal.getActualMaximum(cal.DAY_OF_MONTH); // 獲取本月最大天數  
  • int DAY_OF_YEAR = cal.get(Calendar.DAY_OF_YEAR);//今年的第幾天  
  • int DAY_OF_MONTH = cal.get(Calendar.DAY_OF_MONTH);//本月的第幾天  
  • System.out.println("現在是: "+year+" 年 "+month+" 月 "+day+  
  •         " 日 "+hour+" 時 "+minute+" 分 "+second+" 秒 "+"\nweekday: "+weekday  
  •         +" 本月天數 :"+last+" 今年的第幾天:"+DAY_OF_YEAR+" 本月第幾天:"+DAY_OF_MONTH);  
  •   
  • cal.set(201354134451);//年月日時分秒(月份0代表1月)  ,毫秒不會自動清零  
  • System.out.println(sdf.format(cal.getTime()));  
  • cal.set(Calendar.MILLISECOND, 0);//毫秒清零  
  • System.out.println(sdf.format(cal.getTime()));  
  • cal.set(Calendar.YEAR, 2014);//年    
  • cal.set(Calendar.MONTH, 7);//月(月份0代表1月)    
  • cal.set(Calendar.DATE, 11);//日    
  • cal.set(Calendar.HOUR_OF_DAY, 15);//時    
  • cal.set(Calendar.MINUTE, 33);//分    
  • cal.set(Calendar.SECOND, 32);//秒    
  • System.out.println(sdf.format(cal.getTime()));  
  •   
  • cal.setTime(new Date());  
  • System.out.println(sdf.format(cal.getTime()));  
  •   
  • cal.add(Calendar.YEAR, 1);//年    
  • cal.add(Calendar.MONTH, 1);//月    
  • cal.add(Calendar.DATE, 1);//日    
  • cal.add(Calendar.HOUR_OF_DAY, -1);//時    
  • cal.add(Calendar.MINUTE, 1);//分    
  • cal.add(Calendar.SECOND, 1);//秒    
  • System.out.println(sdf.format(cal.getTime()));  
  •   
  • Calendar calendarNew = Calendar.getInstance();  
  • calendarNew.add(Calendar.HOUR, -2);    
  •    System.out.println("時間比較:" + calendarNew.compareTo(cal));   

  • 免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM