Android Date Compare compareCurrentDate(Date currentDate)

Here you can find the source of compareCurrentDate(Date currentDate)

Description

compare Current Date

Declaration

public static int compareCurrentDate(Date currentDate) 

Method Source Code

//package com.java2s;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static int compareCurrentDate(Date currentDate) {
        return compareDate(currentDate, nowAsDate(), Calendar.DATE);
    }/*from  w w w.  j  a va  2 s .  com*/

    public static int compareCurrentDate(String currentDate)
            throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date objDate = df.parse(currentDate);
        return compareDate(objDate, nowAsDate(), Calendar.DATE);

    }

    public static int compareDate(String date1, String date2, int what)
            throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date objDate1 = df.parse(date1);
        Date objDate2 = df.parse(date2);
        return compareDate(objDate1, objDate2, what);
    }

    public static int compareDate(Date date1, Date date2, int what) {
        Calendar objCalendar1 = Calendar.getInstance();
        objCalendar1.setTime(date1);
        Calendar objCalendar2 = Calendar.getInstance();
        objCalendar2.setTime(date2);
        int iResult = compare(objCalendar1, objCalendar2, what);
        if (iResult > 0) {
            return 1;
        } else if (iResult < 0) {
            return -1;
        } else {
            return 0;
        }
    }

    public static java.sql.Date nowAsDate() {
        Calendar objCurrentCalendar = Calendar.getInstance();
        StringBuffer sbDateString = new StringBuffer();
        int iYear = objCurrentCalendar.get(Calendar.YEAR);
        int iMonth = objCurrentCalendar.get(Calendar.MONTH) + 1;
        int iDate = objCurrentCalendar.get(Calendar.DATE);
        sbDateString.append(iYear);
        sbDateString.append("-");
        sbDateString.append(iMonth);
        sbDateString.append("-");
        sbDateString.append(iDate);
        try {
            return java.sql.Date.valueOf(sbDateString.toString());
        } catch (Exception e) {
            return new java.sql.Date(System.currentTimeMillis());
        }
    }

    public static int compare(Calendar c1, Calendar c2, int what) {
        int number = 0;
        switch (what) {
        case Calendar.YEAR:
            number = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
            break;
        case Calendar.MONTH:
            int years = compare(c1, c2, Calendar.YEAR);
            number = 12 * years
                    + (c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH));
            break;
        case Calendar.DATE:
            number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            break;
        default:
            number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            break;
        }
        return number;
    }

    public static int compare(Timestamp t1, Timestamp t2, int what) {

        Calendar c1 = Calendar.getInstance();
        c1.setTime(t1);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(t2);

        int number = 0;
        switch (what) {
        case Calendar.YEAR:
            number = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
            break;
        case Calendar.MONTH:
            int years = compare(c1, c2, Calendar.YEAR);
            number = 12 * years
                    + (c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH));
            break;
        case Calendar.DATE:
            number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            break;
        default:
            number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            break;
        }
        return number;
    }
}

Related

  1. compareDate(Date date1, Date date2)
  2. compareDate(Date date1, Date date2, int what)
  3. compareDate(String date1, String date2, int what)
  4. dateAfterDate(Date date, long days)