Here you can find the source of compareDate(String d1, String d2)
Parameter | Description |
---|---|
d1 | the d 1 |
d2 | the d 2 |
public static int compareDate(String d1, String d2)
//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; } }