Java Calendar 类的时间操作

原文转载:http://blog.csdn.net/joyous/article/details/9630893

Java Calendar 类时间操作,这也许是创建和管理日历最简单的一个方案,示范代码很简单。

演示了获取时间,日期时间的累加和累减,以及比较。


注意事项:

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. }  


要计算时间差,可用 Calendar.getTimeInMillis() 取得两个时间的微秒级的时间差,再加以换算即可,比如获得相差天数,代码如下:

[java]  view plain  copy
  1. // 得微秒级时间差  
  2. long val = calendarEnd.getTimeInMillis() - calendarBegin.getTimeInMillis();  
  3. // 换算后得到天数  
  4. long day = val / (1000 * 60 * 60 * 24);  


原文转载:http://blog.csdn.net/joyous/article/details/9630893

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值