Java Calendar使用总结

本文介绍 Java 中 Calendar 类的使用方法,包括如何获取时间、日期时间的累加与累减及比较。并提供示例代码展示如何设置和获取日期时间、进行时间比较等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 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 的格式定义

    LetterDate or Time ComponentPresentationExamples
    GEra designatorTextAD
    yYearYear199696
    YWeek yearYear200909
    MMonth in year (context sensitive)MonthJulyJul07
    LMonth in year (standalone form)MonthJulyJul07
    wWeek in yearNumber27
    WWeek in monthNumber2
    DDay in yearNumber189
    dDay in monthNumber10
    FDay of week in monthNumber2
    EDay name in weekTextTuesdayTue
    uDay number of week (1 = Monday, ..., 7 = Sunday)Number1
    aAm/pm markerTextPM
    HHour in day (0-23)Number0
    kHour in day (1-24)Number24
    KHour in am/pm (0-11)Number0
    hHour in am/pm (1-12)Number12
    mMinute in hourNumber30
    sSecond in minuteNumber55
    SMillisecondNumber978
    zTime zoneGeneral time zonePacific Standard TimePSTGMT-08:00
    ZTime zoneRFC 822 time zone-0800
    XTime zoneISO 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. }  
      
  2. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");  
  3. /*初始化*/  
  4. Calendar cal = Calendar.getInstance();  
  5.   
  6. int year = cal.get(Calendar.YEAR);//年    
  7. int month = cal.get(Calendar.MONTH)+1;//月  
  8. int day = cal.get(Calendar.DATE);//日    
  9. int hour = cal.get(Calendar.HOUR_OF_DAY);//时    
  10. int minute = cal.get(Calendar.MINUTE);//分  
  11. int second = cal.get(Calendar.SECOND);//秒    
  12. int weekday = cal.get(Calendar.DAY_OF_WEEK);//星期   周日-周六对应1-7  
  13. int last = cal.getActualMaximum(cal.DAY_OF_MONTH); // 获取本月最大天数  
  14. int DAY_OF_YEAR = cal.get(Calendar.DAY_OF_YEAR);//今年的第几天  
  15. int DAY_OF_MONTH = cal.get(Calendar.DAY_OF_MONTH);//本月的第几天  
  16. System.out.println("现在是: "+year+" 年 "+month+" 月 "+day+  
  17.         " 日 "+hour+" 时 "+minute+" 分 "+second+" 秒 "+"\nweekday: "+weekday  
  18.         +" 本月天数 :"+last+" 今年的第几天:"+DAY_OF_YEAR+" 本月第几天:"+DAY_OF_MONTH);  
  19.   
  20. cal.set(201354134451);//年月日时分秒(月份0代表1月)  ,毫秒不会自动清零  
  21. System.out.println(sdf.format(cal.getTime()));  
  22. cal.set(Calendar.MILLISECOND, 0);//毫秒清零  
  23. System.out.println(sdf.format(cal.getTime()));  
  24. cal.set(Calendar.YEAR, 2014);//年    
  25. cal.set(Calendar.MONTH, 7);//月(月份0代表1月)    
  26. cal.set(Calendar.DATE, 11);//日    
  27. cal.set(Calendar.HOUR_OF_DAY, 15);//时    
  28. cal.set(Calendar.MINUTE, 33);//分    
  29. cal.set(Calendar.SECOND, 32);//秒    
  30. System.out.println(sdf.format(cal.getTime()));  
  31.   
  32. cal.setTime(new Date());  
  33. System.out.println(sdf.format(cal.getTime()));  
  34.   
  35. cal.add(Calendar.YEAR, 1);//年    
  36. cal.add(Calendar.MONTH, 1);//月    
  37. cal.add(Calendar.DATE, 1);//日    
  38. cal.add(Calendar.HOUR_OF_DAY, -1);//时    
  39. cal.add(Calendar.MINUTE, 1);//分    
  40. cal.add(Calendar.SECOND, 1);//秒    
  41. System.out.println(sdf.format(cal.getTime()));  
  42.   
  43. Calendar calendarNew = Calendar.getInstance();  
  44. calendarNew.add(Calendar.HOUR, -2);    
  45.    System.out.println("时间比较:" + calendarNew.compareTo(cal));   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值