Java Utililty Methods Date Between

List of utility methods to do Date Between

Description

The list of methods to do Date Between are organized into topic(s).

Method

intdaysBetween(final Date date1, final Date date2)
days Between
return (int) ((getZeroTimeDate(date1).getTime() - getZeroTimeDate(date2).getTime())
        / (1000 * 60 * 60 * 24));
intdaysBetween(final Date dateA, final Date dateB)
Calculates the number of days that have in common the first interval and the second interval
Date date1, date2;
date1 = dateA;
date2 = dateB;
if (date1.after(date2)) {
    date1 = dateB;
    date2 = dateA;
Calendar calendar1 = GregorianCalendar.getInstance();
...
intdaysBetween(String bdate, String edate)
days Between
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(bdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(edate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
...
intdaysBetween(String beginDate, String endDate)
days Between
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
try {
    cal.setTime(sdf.parse(beginDate));
    cal2.setTime(sdf.parse(endDate));
    long time1 = cal.getTimeInMillis();
    long time2 = cal2.getTimeInMillis();
...
intdaysBetween(String from, String to)
return days between two date strings with user defined format.
return daysBetween(from, to, "yyyyMMdd");
intdaysBetween(String from, String to, String form)
days Between
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(form, java.util.Locale.KOREA);
java.util.Date d1 = null;
java.util.Date d2 = null;
try {
    d1 = formatter.parse(from);
    d2 = formatter.parse(to);
} catch (java.text.ParseException e) {
    return -999;
...
intdaysBetween(String from, String to, String format)
days Between
try {
    java.util.Date d1 = check(from, format);
    java.util.Date d2 = check(to, format);
    long duration = d2.getTime() - d1.getTime();
    return (int) (duration / (1000 * 60 * 60 * 24));
} catch (Exception e) {
    throw new Exception("[DateUtil][daysBetween]" + e.getMessage(), e);
intdaysBetween(String from, String to, String format)
days Between
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.UK);
Calendar calendar = Calendar.getInstance(Locale.UK);
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setMinimalDaysInFirstWeek(4);
Date date1 = null;
Date date2 = null;
try {
    date1 = formatter.parse(from);
...
intdaysBetween(String smdate, String bdate)
days Between
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
...
longdaysBetween2Dates(Date d1, Date d2)
Find out the distance between two dates
Calendar c1 = Calendar.getInstance(); 
Calendar c2 = Calendar.getInstance(); 
c1.setTime(d1);
c2.setTime(d2);
return (c2.getTime().getTime() - c1.getTime().getTime()) / (24 * 3600 * 1000);