Java Date Between daysBetween(String from, String to)

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

Description

return days between two date strings with user defined format.

License

Open Source License

Parameter

Parameter Description
from start date
to end date

Exception

Parameter Description
ParseExceptionerro info

Return

daysBetween value

Declaration

public static int daysBetween(String from, String to) throws ParseException 

Method Source Code


//package com.java2s;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Locale;

public class Main {
    /**//from  w  w w. ja  v  a2 s  .  c o  m
     * return days between two date strings with user defined format.
     * @param from  start date
     * @param to end date
     * @return daysBetween value
     * @throws ParseException  erro info
     */
    public static int daysBetween(String from, String to) throws ParseException {
        return daysBetween(from, to, "yyyyMMdd");
    }

    /**
     * return days between two date strings with user defined format.
     * @param from  start date
     * @param to    end date
     * @param format date format
     * @return daysBetween value
     * @throws ParseException erro info
     */
    public static int daysBetween(String from, String to, String format) throws ParseException {
        Date d1 = check(from, format);
        Date d2 = check(to, format);

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

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

    /**
     *
     * @param s   date string you want to check.
     * @param format string representation of the date format. For example, "yyyy-MM-dd".
     * @return  date Date
     * @throws ParseException error info
     */
    public static Date check(String s, String format) throws ParseException {
        if (s == null)
            throw new ParseException("date string to check is null", 0);
        if (format == null)
            throw new ParseException("format string to check date is null", 0);

        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.KOREA);
        Date date = null;
        try {
            date = formatter.parse(s);
        } catch (ParseException e) {
            throw new ParseException(" wrong date:\"" + s + "\" with format \"" + format + "\"", 0);
        }

        if (!formatter.format(date).equals(s))
            throw new ParseException("Out of bound date:\"" + s + "\" with format \"" + format + "\"", 0);
        return date;
    }
}

Related

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