Java Calendar 类的时间操作

来源:互联网 发布:上海东行网络 编辑:程序博客网 时间:2024/05/02 05:47

Java Calendar 类时间操作,代码很简单。

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


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package MyTest;  
  2.   
  3. /* 
  4.  * 演示 Calendar 的一般操作 
  5.  */  
  6. import java.util.Date;  
  7. import java.text.SimpleDateFormat;  
  8. import java.text.DateFormat;  
  9. import java.util.Calendar;  
  10.   
  11. public class Test  
  12. {  
  13.   public Test()  
  14.   {  
  15.   }  
  16.   
  17.   public static void main(String[] args)  
  18.   {  
  19.     // 字符串转换日期格式  
  20.     // DateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  21.     // 得到日期格式对象  
  22.     // Date date = fmtDateTime.parse(strDateMake);  
  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.     // 初始化 Calendar 对象,但并不必要,除非需要重置时间  
  31.     calendar.setTime(new Date());  
  32.   
  33.     // setTime 类似上面一行  
  34.     // Date date = new Date();  
  35.     // calendar.setTime(date);  
  36.   
  37.     // 显示年份  
  38.     int year = calendar.get(Calendar.YEAR);  
  39.     System.out.println("YEAR is = " + String.valueOf(year));  
  40.   
  41.     // 显示月份 (从0开始, 实际显示要加一)  
  42.     int MONTH = calendar.get(Calendar.MONTH);  
  43.     System.out.println("MONTH is = " + (MONTH + 1));  
  44.   
  45.     // 今年的第 N 天  
  46.     int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);  
  47.     System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR);  
  48.   
  49.     // 本月第 N 天  
  50.     int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);  
  51.     System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH));  
  52.   
  53.     // 3小时以后  
  54.     calendar.add(Calendar.HOUR_OF_DAY, 3);  
  55.     int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);  
  56.     System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY);  
  57.   
  58.     // 当前分钟数  
  59.     int MINUTE = calendar.get(Calendar.MINUTE);  
  60.     System.out.println("MINUTE = " + MINUTE);  
  61.   
  62.     // 15 分钟以后  
  63.     calendar.add(Calendar.MINUTE, 15);  
  64.     MINUTE = calendar.get(Calendar.MINUTE);  
  65.     System.out.println("MINUTE + 15 = " + MINUTE);  
  66.   
  67.     // 30分钟前  
  68.     calendar.add(Calendar.MINUTE, -30);  
  69.     MINUTE = calendar.get(Calendar.MINUTE);  
  70.     System.out.println("MINUTE - 30 = " + MINUTE);  
  71.   
  72.     // 格式化显示  
  73.     str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());  
  74.     System.out.println(str);  
  75.   
  76.     // 重置 Calendar 显示当前时间  
  77.     calendar.setTime(new Date());  
  78.     str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());  
  79.     System.out.println(str);  
  80.   
  81.     // 创建一个 Calendar 用于比较时间  
  82.     Calendar calendarNew = Calendar.getInstance();  
  83.   
  84.     // 设定为 5 小时以前,后者大,显示 -1  
  85.     calendarNew.add(Calendar.HOUR, -5);  
  86.     System.out.println("时间比较:" + calendarNew.compareTo(calendar));  
  87.   
  88.     // 设定7小时以后,前者大,显示 1  
  89.     calendarNew.add(Calendar.HOUR, +7);  
  90.     System.out.println("时间比较:" + calendarNew.compareTo(calendar));  
  91.   
  92.     // 退回 2 小时,时间相同,显示 0  
  93.     calendarNew.add(Calendar.HOUR, -2);  
  94.     System.out.println("时间比较:" + calendarNew.compareTo(calendar));  
  95.   }  
  96. }  
0 0