`

计算两个日期之间相差的天数

阅读更多
程序经过一系列处理,得到两个string类型的时间值,一个值的形式为:“2008-05-09”,一个形式为“2008-5-13”,如何比较他们之间相差的天数?
import java.text.DateFormat;      
import java.text.ParseException;      
import java.text.SimpleDateFormat;      
import java.util.Calendar;      
import java.util.Date;      
     
public class CompareTime {      
     
    public static void main(String[] args) {      
        String t1 = "2009-02-03";      
        String t2 = "2009-04-28";      
     
        try {      
            System.out.println(CompareTime.getBetweenDays(t1, t2));      
        } catch (ParseException e) {      
            e.printStackTrace();      
        }      
    }      
     
    /**    
     * 取得两个时间段的时间间隔    
     *     
     * @author color    
     * @param t1    
     *            时间1    
     * @param t2    
     *            时间2    
     * @return t2 与t1的间隔天数    
     * @throws ParseException    
     *             如果输入的日期格式不是0000-00-00 格式抛出异常    
     */     
    public static int getBetweenDays(String t1, String t2)      
            throws ParseException {      
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");      
        int betweenDays = 0;      
        Date d1 = format.parse(t1);      
        Date d2 = format.parse(t2);      
        Calendar c1 = Calendar.getInstance();      
        Calendar c2 = Calendar.getInstance();      
        c1.setTime(d1);      
        c2.setTime(d2);      
        // 保证第二个时间一定大于第一个时间      
        if (c1.after(c2)) {      
            c1 = c2;      
            c2.setTime(d1);      
        }      
        int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);      
        betweenDays = c2.get(Calendar.DAY_OF_YEAR)      
                - c1.get(Calendar.DAY_OF_YEAR);      
        for (int i = 0; i < betweenYears; i++) {      
            c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));      
            betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);      
        }      
        return betweenDays;      
    }      
     
}    

import java.text.DateFormat;   
import java.text.ParseException;   
import java.text.SimpleDateFormat;   
import java.util.Calendar;   
import java.util.Date;   
  
public class CompareTime {   
  
    public static void main(String[] args) {   
        String t1 = "2009-02-03";   
        String t2 = "2009-04-28";   
  
        try {   
            System.out.println(CompareTime.getBetweenDays(t1, t2));   
        } catch (ParseException e) {   
            e.printStackTrace();   
        }   
    }   
  
    /**  
     * 取得两个时间段的时间间隔  
     *   
     * @author color  
     * @param t1  
     *            时间1  
     * @param t2  
     *            时间2  
     * @return t2 与t1的间隔天数  
     * @throws ParseException  
     *             如果输入的日期格式不是0000-00-00 格式抛出异常  
     */  
    public static int getBetweenDays(String t1, String t2)   
            throws ParseException {   
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
        int betweenDays = 0;   
        Date d1 = format.parse(t1);   
        Date d2 = format.parse(t2);   
        Calendar c1 = Calendar.getInstance();   
        Calendar c2 = Calendar.getInstance();   
        c1.setTime(d1);   
        c2.setTime(d2);   
        // 保证第二个时间一定大于第一个时间   
        if (c1.after(c2)) {   
            c1 = c2;   
            c2.setTime(d1);   
        }   
        int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);   
        betweenDays = c2.get(Calendar.DAY_OF_YEAR)   
                - c1.get(Calendar.DAY_OF_YEAR);   
        for (int i = 0; i < betweenYears; i++) {   
            c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));   
            betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);   
        }   
        return betweenDays;   
    }   
  
}  
 



int day=(int)((d2.getTime()-d1.getTime())/86400000L);

这个多简单呀.  一行代码就OK了.

取毫秒相减,在转成天.......
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics