Java Date Between daysBetween(String from, String to, String form)

Here you can find the source of daysBetween(String from, String to, String form)

Description

days Between

License

Open Source License

Declaration

public static final int daysBetween(String from, String to, String form) 

Method Source Code

//package com.java2s;

public class Main {

    public static final int daysBetween(String from, String to, String form) {
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(form, java.util.Locale.KOREA);
        java.util.Date d1 = null;
        java.util.Date d2 = null;
        try {/*from   w ww .  j a va 2  s  . c  o  m*/
            d1 = formatter.parse(from);
            d2 = formatter.parse(to);
        } catch (java.text.ParseException e) {
            return -999;
        }
        if (!formatter.format(d1).equals(from))
            return -999;
        if (!formatter.format(d2).equals(to))
            return -999;

        long duration = d2.getTime() - d1.getTime();

        if (duration < 0)
            return -999;

        return (int) (duration / (1000 * 60 * 60 * 24));
        // seconds in 1 day
    }
}

Related

  1. daysBetween(final Date date1, final Date date2)
  2. daysBetween(final Date dateA, final Date dateB)
  3. daysBetween(String bdate, String edate)
  4. daysBetween(String beginDate, String endDate)
  5. daysBetween(String from, String to)
  6. daysBetween(String from, String to, String format)
  7. daysBetween(String from, String to, String format)
  8. daysBetween(String smdate, String bdate)
  9. daysBetween2Dates(Date d1, Date d2)