Java Date Compare compareDate(String d1, String d2)

Here you can find the source of compareDate(String d1, String d2)

Description

Compare date int.

License

Apache License

Parameter

Parameter Description
d1 the d 1
d2 the d 2

Return

int int

Declaration

public static int compareDate(String d1, String d2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.DateFormat;

import java.util.Date;

import java.util.GregorianCalendar;

public class Main {
    /**/*from  w ww  .  j  ava 2 s . co m*/
     * Compare date int.
     *
     * @param d1 the d 1
     * @param d2 the d 2
     * @return int int
     */
    public static int compareDate(String d1, String d2) {
        short vl = 1;
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(getStrDate(d1));
        int year = gc.get(GregorianCalendar.YEAR);
        int month = gc.get(GregorianCalendar.MONTH);
        int day = gc.get(GregorianCalendar.DAY_OF_MONTH);
        gc.setTime(getStrDate(d2));
        int tempYear = gc.get(GregorianCalendar.YEAR);
        int tempMonth = gc.get(GregorianCalendar.MONTH);
        int tempDay = gc.get(GregorianCalendar.DAY_OF_MONTH);
        if (year != tempYear) {
            if (year > tempYear)
                vl = 2;
            else
                vl = 0;
        } else {
            if (month != tempMonth) {
                if (month > tempMonth)
                    vl = 2;
                else
                    vl = 0;
            } else {
                if (day != tempDay) {
                    if (day > tempDay)
                        vl = 2;
                    else
                        vl = 0;
                }
            }
        }
        return vl;
    }

    /**
     * Gets the str date.
     *
     * @param strX the str X
     * @return the str date
     */
    public static Date getStrDate(String strX) {
        Date date1 = new Date();
        if (!strX.equals("")) {
            try {
                date1 = (DateFormat.getDateInstance()).parse(strX);
            } catch (Exception ex) {

                System.out.println(ex.toString());
            }
        } else {
            GregorianCalendar gcNow = new GregorianCalendar();
            date1 = gcNow.getTime();
        }

        return date1;
    }
}

Related

  1. compareDate(Date date1, Date date2, String pattern)
  2. compareDate(Date first, Date second)
  3. compareDate(Date sourceDate, Date targetDate)
  4. compareDate(Date start, Date end)
  5. compareDate(String begingDate, String endDate, String format)
  6. compareDate(String d1, String d2)
  7. compareDate(String date)
  8. compareDate(String date0, String date1)
  9. compareDate(String date1, File file)