Java Day getDaysFrom2Dates(Calendar calendar1, Calendar calendar2)

Here you can find the source of getDaysFrom2Dates(Calendar calendar1, Calendar calendar2)

Description

get Days From Dates

License

Open Source License

Declaration

public static int getDaysFrom2Dates(Calendar calendar1, Calendar calendar2) 

Method Source Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    private static final String PATTERN = "yyyy-MM-dd";
    private static final Long MILLISECOND_DAY = 86400000L;

    public static int getDaysFrom2Dates(Calendar calendar1, Calendar calendar2) {
        return (int) (Math.abs(calendar1.getTimeInMillis() - calendar2.getTimeInMillis()) / MILLISECOND_DAY);
    }//ww  w.  j a  v  a  2s.c o m

    public static int getDaysFrom2Dates(Date date1, Date date2) {
        return (int) (Math.abs(date1.getTime() - date2.getTime()) / MILLISECOND_DAY);
    }

    public static int getDaysFrom2Dates(String date1, String date2, String pattern) throws ParseException {
        return getDaysFrom2Dates(parseString2Date(date1, pattern), parseString2Date(date2, pattern));
    }

    public static int getDaysFrom2Dates(String date1, String date2) throws ParseException {
        return getDaysFrom2Dates(date1, date2, null);
    }

    public static Date parseString2Date(String date, String pattern) throws ParseException {
        return getSimpleDateFormatInstance(pattern).parse(date);
    }

    public static Date parseString2Date(String date) throws ParseException {
        return parseString2Date(date, null);
    }

    private static SimpleDateFormat getSimpleDateFormatInstance(String pattern) {
        return new SimpleDateFormat(pattern == null ? PATTERN : pattern);
    }
}

Related

  1. getDays(String yyyy)
  2. getDaysAgo(int interval)
  3. getDaysByYearMonth(String ym)
  4. getDaysForDate(String date)
  5. getDaysforYear()
  6. getDaysLater(int day)
  7. getDaysMonthYear(Date date)
  8. getDaysOfDates(Date first, Date second)
  9. getdaysslice(String startdate, String enddate)