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
 
  在CODE上查看代码片 派生到我的代码片
  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
 
  在CODE上查看代码片 派生到我的代码片
  1. // 得微秒级时间差  
  2. long val = calendarEnd.getTimeInMillis() - calendarBegin.getTimeInMillis();  
  3. // 换算后得到天数  
  4. long day = val / (1000 * 60 * 60 * 24);  
public  class  Test {
 
     public  static  void  main(String[] args)  throws  Exception {
         final  SimpleDateFormat sdf = new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
         long  period= 1000 * 60 * 24 ; //间隔执行时间
         //设置时间为当天12点钟
         Calendar cal = Calendar.getInstance();
         cal.set(Calendar.HOUR_OF_DAY,  12 );
         cal.set(Calendar.MINUTE,  0 );
         cal.set(Calendar.SECOND,  0 );
         //如果当天时间超过12点,定时器启动时间设置为下一天
         if ( new  Date().getTime() > cal.getTimeInMillis()){
             cal.add(Calendar.DAY_OF_YEAR,  1 );
         }
         //System.out.println(sdf.format(cal.getTime()));
         Timer timer =  new  Timer();
         timer.schedule( new  TimerTask() {
             @Override
             public  void  run() {
                 Date now =  new  Date();
                 System.out.println(sdf.format(now)+ "\t helloworld" );
             }
         },cal.getTime(),period);
     }
}

猜你喜欢

转载自lzazmy.iteye.com/blog/2347915