时间类Calendar类操作 创建和管理日历时间

来源:互联网 发布:失去的胜利 知乎 编辑:程序博客网 时间:2024/05/17 21:55

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

java Calendar 演示代码如下:

[java] view plain copy
print?
  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.   
原创粉丝点击