Java Day From getDaysBetween(int dateInt1, int dateInt2)

Here you can find the source of getDaysBetween(int dateInt1, int dateInt2)

Description

gets days between any two dates (integer form) regardless of order

License

Open Source License

Parameter

Parameter Description
dateInt1 first date (integer)
dateInt2 second date (integer)

Return

number of days between dates

Declaration

public static int getDaysBetween(int dateInt1, int dateInt2) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    private static final long MillisPerDay = (24 * 60 * 60 * 1000);
    private static HashMap<Integer, Calendar> ctcMemo = new HashMap<>();

    /**// w ww  .j  a v a2  s . c  o  m
     * gets days between any two dates (integer form) regardless of order
     *
     * @param dateInt1 first date (integer)
     * @param dateInt2 second date (integer)
     * @return number of days between dates
     */
    public static int getDaysBetween(int dateInt1, int dateInt2) {
        Calendar di1 = convertToCal(dateInt1);
        Calendar di2 = convertToCal(dateInt2);

        return Math.round(Math.abs(di1.getTimeInMillis() - di2.getTimeInMillis()) / (float) MillisPerDay);
    }

    /**
     * converts dateInt to Calendar
     *
     * @param dateInt date int to convert
     * @return calendar value
     */
    private static Calendar convertToCal(int dateInt) {
        Calendar result = ctcMemo.get(dateInt);
        if (result != null) {
            return result;
        }

        Calendar gc = new GregorianCalendar();
        gc.setTime(convertToDate(dateInt));

        ctcMemo.put(dateInt, gc);
        return gc;
    }

    /**
     * converts dateInto to date
     *
     * @param dateInt dateInt to be converted
     * @return date
     */
    public static Date convertToDate(int dateInt) {
        @SuppressWarnings("deprecation")
        Date nd = new Date(dateInt / 10000 - 1900, (dateInt / 100) % 100 - 1, dateInt % 100);

        return nd;
    }
}

Related

  1. getDay(String date)
  2. getDay0(Date date)
  3. getDayOfDate(Date date)
  4. getDays(Date date1, Date date2)
  5. getDays(Date sd, Date ed)
  6. getDaysInPastForDate(Date date)
  7. getFirstMilliOfDay(Date date)
  8. getIntervalInDaysSofar(Date from)
  9. getLastDay(Date date)