Java Day Between getDifferenceOfDays(String dateFromStr, String dateToStr, String dateFormat)

Here you can find the source of getDifferenceOfDays(String dateFromStr, String dateToStr, String dateFormat)

Description

get Difference Of Days

License

Open Source License

Declaration

public static int getDifferenceOfDays(String dateFromStr, String dateToStr, String dateFormat) 

Method Source Code

//package com.java2s;

import java.text.*;

import java.util.TimeZone;

public class Main {
    public static final String DEFAULT_CHAR_DATE_YMD_FORMAT = "yyyyMMdd";

    public static int getDifferenceOfDays(java.util.Date dateFrom, java.util.Date dateTo) {

        return new Long((dateTo.getTime() - dateFrom.getTime()) / 1000 / 60 / 60 / 24).intValue();
    }/* w w w. ja  v  a 2  s .c o m*/

    public static int getDifferenceOfDays(String dateFromStr, String dateToStr, String dateFormat) {

        java.util.Date dateFrom = parseDate(dateFromStr, dateFormat);
        java.util.Date dateTo = parseDate(dateToStr, dateFormat);

        return getDifferenceOfDays(dateFrom, dateTo);
    }

    public static int getDifferenceOfDays(String dateFromStr, String dateToStr) {

        return getDifferenceOfDays(dateFromStr, dateToStr, DEFAULT_CHAR_DATE_YMD_FORMAT);
    }

    public static java.util.Date parseDate(String value, String pattern) {
        try {
            TimeZone tz = TimeZone.getDefault();
            String dateFormat = pattern;
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            sdf.setTimeZone(tz);

            // Parse date
            java.util.Date parsed = null;

            parsed = sdf.parse(value);
            return parsed;
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. getDiffDays(String startDate, String endDate)
  2. getDifferDays(Date d1, Date d2)
  3. getDifferDays(String date1, String date2)
  4. getDifference(Date d1, Date d2)
  5. getDifferenceInDays(final Date startDate, final Date endDate)
  6. getDifferencesBetweenIndicateDays( final Date minorDate, final Date majorDate)
  7. getDiffMilliSeconds(Date form, Date to)
  8. getManyWeeksDifference(Date a, Date b)
  9. getMinuteDiffByTime(Date time1, Date time2)