Java Date Between daysBetween(Date smdate, Date bdate)

Here you can find the source of daysBetween(Date smdate, Date bdate)

Description

days Between

License

Open Source License

Declaration

public static Integer daysBetween(Date smdate, Date bdate) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static Integer daysBetween(Date smdate, Date bdate) {
        try {//from   w w w .j  a v  a 2s  .  com
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);
            return Integer.parseInt(String.valueOf(between_days));
        } catch (Exception e) {
            return null;
        }
    }

    public static String format(java.util.Date date, String format) {
        String result = "";
        try {
            if (date != null) {
                java.text.DateFormat df = new java.text.SimpleDateFormat(format);
                result = df.format(date);
            }
        } catch (Exception e) {
        }
        return result;
    }

    public static String format(java.util.Date date) {
        return format(date, "yyyy/MM/dd");
    }
}

Related

  1. betweenTwoDates(Date date1, Date date2)
  2. daysBetween(Date d1)
  3. daysBetween(Date d1, Date d2)
  4. DaysBetween(Date date1, Date date2)
  5. daysBetween(Date date1, Date date2)
  6. daysBetween(Date smdate, Date bdate)
  7. daysBetween(Date smdate, Date bdate)
  8. daysBetween(Date smdate, Date bdate)
  9. daysBetween(Date startDate, Date endDate)