Java 计算两个日期相差月数、天数

时间:2024-02-22 08:17:32
package com.myjava;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MyCalendar {

    /**
     * @param args
     * @throws ParseException
     */
    public static void main(String[] args) throws ParseException {
        System.out.println(MyCalendar.getMonthSpace("1982-12-8", "2012-12-12"));

    }

/**
     * 
     * @param date1 <String>
     * @param date2 <String>
     * @return int
     * @throws ParseException
     */
    public static int getMonthSpace(String date1, String date2)
            throws ParseException {

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

      int result=0;
      Calendar cal1 = new GregorianCalendar();
      cal1.setTime(sdf.parse(date1));
      cal2 = new GregorianCalendar();
      cal2.setTime(sdf.parse(date2));
      result =(cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12 + cal1.get(Calendar.MONTH)- cal2.get(Calendar.MONTH);
      return result==0?1 : Math.abs(result);

    }

}
复制代码

 

复制代码
     * @param date1 <String>
     * @param date2 <String>
     * @return int
     * @throws ParseException
     */
    public int getDateSpace(String date1, String date2)
            throws ParseException {

        int result = 0;

        Calendar calst = Calendar.getInstance();;
        Calendar caled = Calendar.getInstance();

        calst.setTime(parseDate("yyyyMMdd",date1));
        caled.setTime(parseDate("yyyyMMdd",date2));
 
         //设置时间为0时   
         calst.set(Calendar.HOUR_OF_DAY, 0);   
         calst.set(Calendar.MINUTE, 0);   
         calst.set(Calendar.SECOND, 0);   
         caled.set(Calendar.HOUR_OF_DAY, 0);   
         caled.set(Calendar.MINUTE, 0);   
         caled.set(Calendar.SECOND, 0);   
        //得到两个日期相差的天数   
         int days = ((int)(caled.getTime().getTime()/1000)-(int)(calst.getTime().getTime()/1000))/3600/24;   
         
        return days;   
    }
复制代码

 

复制代码
public static final int daysBetween(Date early, Date late) { 
     
         java.util.Calendar calst = java.util.Calendar.getInstance();   
         java.util.Calendar caled = java.util.Calendar.getInstance();   
         calst.setTime(early);   
          caled.setTime(late);   
          //设置时间为0时   
          calst.set(java.util.Calendar.HOUR_OF_DAY, 0);   
          calst.set(java.util.Calendar.MINUTE, 0);   
          calst.set(java.util.Calendar.SECOND, 0);   
          caled.set(java.util.Calendar.HOUR_OF_DAY, 0);   
          caled.set(java.util.Calendar.MINUTE, 0);   
          caled.set(java.util.Calendar.SECOND, 0);   
         //得到两个日期相差的天数   
          int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst   
                 .getTime().getTime() / 1000)) / 3600 / 24;   
          
         return days;   
    }   
   
    
    public static void main(String[] args)
    {
         Date earlydate = new Date();   
         Date latedate = new Date();   
         DateFormat df = DateFormat.getDateInstance();   
         try {   
             earlydate = df.parse("2009-09-21");   
             latedate = df.parse("2009-10-16");   
         } catch (ParseException e) {   
               e.printStackTrace();   
           }   
          int days = daysBetween(earlydate,latedate);   
          System.out.println(days);   
    }