java日期处理(五)

        在java日期时间类型对象的应用中最常见也是最容易出错的应用是对日期和时间变量进行相互之间的计算操作,我们总结了一下,大概可以归结为以下操作类型。

1、计算两个日期之间的差(用毫秒、秒、分钟、小时、天表示)
2、在某个日期的基础上加上几天或者几小时后的日期

我们还是先看代码

Code:
  1.   
  2. /**  
  3.  * 日期之间的求差的计算  
  4.  *  
  5.  */  
  6. public static void diffDate(){   
  7.        
  8.        
  9.     Date currentDate;//当前系统日期   
  10.     Date firstDate; //用于比较的日期   
  11.     Calendar currentCalendar;   
  12.     Calendar firstCalendar;   
  13.        
  14.     //创建日期对象   
  15.     currentDate = new Date();   
  16.     firstDate = getFormatString2Date("2009-05-29""yyyy-MM-dd");   
  17.        
  18.     //将Date对象转化成Calendar对象以便于计算   
  19.     currentCalendar = Calendar.getInstance();   
  20.     firstCalendar = Calendar.getInstance();   
  21.        
  22.     currentCalendar.setTime(currentDate);   
  23.     firstCalendar.setTime(firstDate);   
  24.        
  25.     long diffmills = currentCalendar.getTimeInMillis() -  firstCalendar.getTimeInMillis();   
  26.     long diffm = diffmills/1000;   
  27.     long diffs = diffmills/(1000*60);   
  28.     long diffh = diffmills/(1000*60*60);   
  29.     long diffd = diffmills/(1000*60*60*24);   
  30.        
  31.     System.out.println(diffmills + "  毫秒");   
  32.     System.out.println(diffm + "  秒");   
  33.     System.out.println(diffs + "  分钟");   
  34.     System.out.println(diffh + "  小时");   
  35.     System.out.println(diffd + "  天");   
  36.        
  37. }   
  38.   
  39.   
  40.   
  41.   
  42. /**  
  43.  * 某个日期加上某个时间单元后的日期  
  44.  *  
  45.  */  
  46. public static void addDate(){   
  47.        
  48.     Date currentDate;//当前系统日期   
  49.     Calendar currentCalendar;   
  50.        
  51.     //创建日期对象   
  52.     currentDate = new Date();   
  53.     //将Date对象转化成Calendar对象以便于计算   
  54.     currentCalendar = Calendar.getInstance();   
  55.   
  56.     //currentDate加上100毫秒后到日期   
  57.     currentCalendar.setTime(currentDate);   
  58.     currentCalendar.add(Calendar.MILLISECOND, 100);   
  59.     Date addDateMills = currentCalendar.getTime();   
  60.        
  61.     //currentDate加上50秒后到日期   
  62.     currentCalendar.setTime(currentDate);   
  63.     currentCalendar.add(Calendar.SECOND, 50);   
  64.     Date addDateS = currentCalendar.getTime();   
  65.        
  66.     //currentDate加上20分钟后到日期   
  67.     currentCalendar.setTime(currentDate);   
  68.     currentCalendar.add(Calendar.MINUTE, 20);   
  69.     Date addDateM = currentCalendar.getTime();   
  70.        
  71.     //currentDate加上一个小时后到日期   
  72.     currentCalendar.setTime(currentDate);   
  73.     currentCalendar.add(Calendar.HOUR_OF_DAY, 1);   
  74.     Date addDateH = currentCalendar.getTime();   
  75.        
  76.     //currentDate加上一天后的日期   
  77.     currentCalendar.setTime(currentDate);   
  78.     currentCalendar.add(Calendar.DAY_OF_YEAR, 1);   
  79.     Date addDateDay = currentCalendar.getTime();   
  80.        
  81.     //currentDate加上一周后的日期   
  82.     currentCalendar.setTime(currentDate);   
  83.     currentCalendar.add(Calendar.WEEK_OF_YEAR, 1);   
  84.     Date addDateWeek = currentCalendar.getTime();   
  85.        
  86.     //currentDate加上一个月后的日期   
  87.     currentCalendar.setTime(currentDate);   
  88.     currentCalendar.add(Calendar.MONTH, 1);   
  89.     Date addDateMonth = currentCalendar.getTime();   
  90.        
  91.        
  92.     String currentDateString = getDate2FormatString(currentDate,"yyyy-MM-dd HH:mm:ss S");   
  93.        
  94.     System.out.println("日期:"+currentDateString + " 加上" + " 100 毫秒 后的日期为 -> "+getDate2FormatString(addDateMills,"yyyy-MM-dd HH:mm:ss S"));   
  95.     System.out.println("日期:"+currentDateString + " 加上" + "  50   秒 后的日期为 -> "+getDate2FormatString(addDateS,"yyyy-MM-dd HH:mm:ss S"));   
  96.     System.out.println("日期:"+currentDateString + " 加上" + "  20 分钟 后的日期为 -> "+getDate2FormatString(addDateM,"yyyy-MM-dd HH:mm:ss S"));   
  97.     System.out.println("日期:"+currentDateString + " 加上" + "   1 小时 后的日期为 -> "+getDate2FormatString(addDateH,"yyyy-MM-dd HH:mm:ss S"));   
  98.     System.out.println("日期:"+currentDateString + " 加上" + "   1   天 后的日期为 -> "+getDate2FormatString(addDateDay,"yyyy-MM-dd HH:mm:ss S"));   
  99.     System.out.println("日期:"+currentDateString + " 加上" + "   1   周 后的日期为 -> "+getDate2FormatString(addDateWeek,"yyyy-MM-dd HH:mm:ss S"));   
  100.     System.out.println("日期:"+currentDateString + " 加上" + "   1   月 后的日期为 -> "+getDate2FormatString(addDateMonth,"yyyy-MM-dd HH:mm:ss S"));   
  101.        
  102. }   
  103.   
  104.   
  105. /**  
  106.  * 格式化日期时间信息  
  107.  * @param userDate       日期对象  
  108.  * @param formatString   格式haunted字符串  
  109.  * @return  
  110.  */  
  111. public static String getDate2FormatString(Date userDate, String formatString) {   
  112.   
  113.     StringBuffer dateformatstring = new StringBuffer();   
  114.   
  115.     // check formatString whether null   
  116.     if ((null == formatString) && (formatString.length() < 0))   
  117.         throw new DateUtilException(   
  118.                 "Function getDate2FormatString error : not find date format");   
  119.   
  120.     // check userDate whether   
  121.     if (null == userDate)   
  122.         return null;   
  123.     SimpleDateFormat dateFormat = new SimpleDateFormat(formatString);   
  124.     dateFormat.format(userDate, dateformatstring, new FieldPosition(0));   
  125.   
  126.     return dateformatstring.toString();   
  127. }   
  128.   
  129.   
  130.   
  131.   
  132. /**  
  133.  * 将日期字符串转化为日期对象  
  134.  * @param str   日期字符串  
  135.  * @param dtfm  格式字符串  
  136.  * @return  
  137.  */  
  138. public static Date getFormatString2Date(String str, String dtfm) {   
  139.        return new Date();
  140.     }  

通过上述代码我们可以发现,其实时间日期相关的增减操作不是很复杂,只要掌握的Calendar对象的一些基本用法,基本上能很快的计算出来,上述代码中getFormatString2Date方法留给大家自己实现,根据我们前面所学的知识应该很容易的就可以实现。

 

 如果您对我的文章感兴趣的话,请点击这里加我为好友,让我们一起进步
 
http://student.youkuaiyun.com/invite.php?u=106708&c=2383a3846076c876

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值