查看完整版本: [-- Java比较两个时间相差多少天,多少个月,多少年 --]

风信Java论坛 ›› Java 基础开发 ›› Java比较两个时间相差多少天,多少个月,多少年 登录 -> 注册

1F Java比较两个时间相差多少天,多少个月,多少年   jastby Post by : 2009-06-09 11:44:04.0

在项目开发当中,我们时常碰到要比较两个时间或者与当前时间相差多少天,多少个月,多少年的问题。

本人结合网上的一些例子,稍作修改,提供出下面示例,与网友们分享。

具体实现方法见注释。如果有更好的方法,请回帖提出,感谢!

   java代码
  1. package com.test;  
  2.  
  3. import java.text.DateFormat;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Calendar;  
  6. import java.util.Date;  
  7.  
  8. /**  
  9.  * @description 日期比较天 月 年  
  10.  * @author www.javawind.net  
  11.  */ 
  12.  
  13. public class DateTest {  
  14.  
  15.     public static void main(String[] args) {  
  16.         String date = "2008-06-12";  
  17.           
  18.         DateTest.compareDate(date, null0);  
  19.         DateTest.compareDate(date, null1);  
  20.         DateTest.compareDate(date, null2);  
  21.           
  22.         date = "2006-06-03";          
  23.         DateTest.compareDate(date, null0);  
  24.         DateTest.compareDate(date, null1);  
  25.         DateTest.compareDate(date, null2);  
  26.         DateTest.compareDate(date, "2009-06-01"0);  
  27.         DateTest.compareDate(date, "2009-06-01"1);  
  28.         DateTest.compareDate(date, "2009-06-01"2);  
  29.     }  
  30.       
  31.     /**  
  32.      * @param date1 需要比较的时间 不能为空(null),需要正确的日期格式  
  33.      * @param date2 被比较的时间  为空(null)则为当前时间  
  34.      * @param stype 返回值类型   0为多少天,1为多少个月,2为多少年  
  35.      * @return  
  36.      */ 
  37.     public static int compareDate(String date1,String date2,int stype){  
  38.         int n = 0;  
  39.           
  40.         String[] u = {"天","月","年"};  
  41.         String formatStyle = stype==1?"yyyy-MM":"yyyy-MM-dd";  
  42.           
  43.         date2 = date2==null?DateTest.getCurrentDate():date2;  
  44.           
  45.         DateFormat df = new SimpleDateFormat(formatStyle);  
  46.         Calendar c1 = Calendar.getInstance();  
  47.         Calendar c2 = Calendar.getInstance();  
  48.         try {  
  49.             c1.setTime(df.parse(date1));  
  50.             c2.setTime(df.parse(date2));  
  51.         } catch (Exception e3) {  
  52.             System.out.println("wrong occured");  
  53.         }  
  54.         //List list = new ArrayList();  
  55.         while (!c1.after(c2)) {                     // 循环对比,直到相等,n 就是所要的结果  
  56.             //list.add(df.format(c1.getTime()));    // 这里可以把间隔的日期存到数组中 打印出来  
  57.             n++;  
  58.             if(stype==1){  
  59.                 c1.add(Calendar.MONTH, 1);          // 比较月份,月份+1  
  60.             }  
  61.             else{  
  62.                 c1.add(Calendar.DATE, 1);           // 比较天数,日期+1  
  63.             }  
  64.         }  
  65.           
  66.         n = n-1;  
  67.           
  68.         if(stype==2){  
  69.             n = (int)n/365;  
  70.         }     
  71.           
  72.         System.out.println(date1+" -- "+date2+" 相差多少"+u[stype]+":"+n);        
  73.         return n;  
  74.     }  
  75.       
  76.     /**  
  77.      * 得到当前日期  
  78.      * @return  
  79.      */ 
  80.     public static String getCurrentDate() {  
  81.         Calendar c = Calendar.getInstance();  
  82.         Date date = c.getTime();  
  83.         SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");  
  84.         return simple.format(date);  
  85.  
  86.     }  

运行结果:

2008-06-12 -- 2009-06-09 相差多少天:362
2008-06-12 -- 2009-06-09 相差多少月:12
2008-06-12 -- 2009-06-09 相差多少年:0
2006-06-03 -- 2009-06-09 相差多少天:1102
2006-06-03 -- 2009-06-09 相差多少月:36
2006-06-03 -- 2009-06-09 相差多少年:3
2006-06-03 -- 2009-06-01 相差多少天:1094
2006-06-03 -- 2009-06-01 相差多少月:36
2006-06-03 -- 2009-06-01 相差多少年:2


风信Java论坛 ›› Java 基础开发 ›› Java比较两个时间相差多少天,多少个月,多少年 登录 -> 注册

查看完整版本: [-- Java比较两个时间相差多少天,多少个月,多少年 --]
CopyRight © 2008-2009 JavaWind.Net Studio All Rights Reserved
Powered By JWind.BBS Vesion 1.0.0 Beta1 Processed in 5 ms,0 (Queries)  Gzip enabled
粤ICP备07511478号