Java Day Between diffInDays(Date date1, Date date2)

Here you can find the source of diffInDays(Date date1, Date date2)

Description

diff In Days

License

Open Source License

Declaration

public static Long diffInDays(Date date1, Date date2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.util.Date;

import java.util.concurrent.TimeUnit;

public class Main {
    static final String DATEFORMAT = "yyyy-MM-dd";

    public static Long diffInDays(Date date1, Date date2) {
        try {/* ww  w  . j  ava 2 s .com*/
            SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);

            String lastUpdateInUTCString = sdf.format(date1);
            String nowInUTCString = sdf.format(date2);

            Date lastUpdateInUTC;

            lastUpdateInUTC = stringDateToDate(lastUpdateInUTCString);

            Date nowInUTC = stringDateToDate(nowInUTCString);

            long nowInUTCMilis = nowInUTC.getTime();
            long lastUpdateInUTCMilis = lastUpdateInUTC.getTime();

            long diff = nowInUTCMilis - lastUpdateInUTCMilis;

            TimeUnit tu = TimeUnit.DAYS;
            long diffinDays = tu.convert(diff, TimeUnit.MILLISECONDS);
            return diffinDays;
        } catch (ParseException e) {
            return Long.MIN_VALUE;
        }

    }

    public static String format(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
        return dateFormat.format(date);
    }

    public static Date stringDateToDate(String StrDate) throws ParseException {
        Date dateToReturn = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);

        try {
            dateToReturn = (Date) dateFormat.parse(StrDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return dateToReturn;
    }
}

Related

  1. countDiffDay(String beginDateBase, String endDateBase)
  2. dayDiff(Date d1, Date d2)
  3. dayDiff(Date start, Date end)
  4. daysDiff(final Date data1, final Date data2)
  5. differentDaysByMillisecond(Date date1, Date date2)
  6. getBeteenDays(String begin, String end, String format)
  7. getBetweenDays(long longValue1, long longValue2)
  8. getBetweenDays(String start, String end)
  9. getBetweenTwoDayCalender(String startDay, String endDay)