Saturday, May 25, 2013

Get day difference between two dates

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
   /**
         * @param date1
         * @param date2
         * @return
         */
        public static long getDayDifference(Date date1, Date date2) {

                long diffDays = 0;
                if(null != date1 && null != date2){
                        Calendar c1 = Calendar.getInstance();
                        c1.setTime(date1);

                        Calendar c2 = Calendar.getInstance();
                        c2.setTime(date2);
                        double milliseconds1 = c1.getTimeInMillis();
                        double milliseconds2 = c2.getTimeInMillis();
                        double diff = milliseconds2 - milliseconds1;
                        diffDays = (long) diff / (24 * 60 * 60 * 1000);
                      
                }
                return diffDays;
        }

No comments:

Post a Comment