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

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 {

        int result = 0;

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

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(sdf.parse(date1));
        c2.setTime(sdf.parse(date2));

        result = c2.get(Calendar.MONTH) - c1.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);   
    }


你可能感兴趣的:(Java 计算两个日期相差月数、天数)