public static int getCompareResult1(String date) throws ParseException {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
df.setLenient ( false );
DateFormat df1 = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyy", Locale.getDefault());
df1.setLenient ( false );
Date d1 = df.parse(date);
Date now = df1.parse(new Date().toString());
Calendar today = Calendar.getInstance(Locale.getDefault());
today.setTime(now);
Calendar compare = Calendar.getInstance(Locale.getDefault());
compare.setTime(d1);
compare.set(Calendar.HOUR_OF_DAY, 0);
compare.set(Calendar.MINUTE, 0);
compare.set(Calendar.SECOND, 0);
compare.set(Calendar.MILLISECOND, 0);
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
if (today.equals(compare)) {
return 0;
} else if(today.before(compare)) {
return -1;
} else if(today.after(compare)) {
return 1;
}
}
part of Util.java
/**
This method return the date in "day/month/year" numerically
@param date "May , 2003" format
@param day in numeric format
@return String "day/month/year"
*/
public static String stringDate(String date, String day) {
StringTokenizer st = new StringTokenizer(date);
String month = st.nextToken(" , ");
String year = st.nextToken();
int monthInt = getMonthInt(month) + 1;
// int monthInt = Integer.parseInt(month);
// monthInt++;
if(monthInt < 10)
return day + "/" + "0"+monthInt+ "/" + year;
else
return day + "/" + monthInt + "/" + year;
}
// public static class CompareDates{
public String getCompareResult(String date) throws ParseException {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");//error line1
System.out.println(date);
Date d1 = df.parse(date);
Date today = new Date();
String relation;
if (d1.equals(today))
relation = "the same as date";
else if(d1.before(today))
relation = "before";
else
relation = "after";
return df.format(d1) + "is" + relation +' '+ df.format(today);
}
// public static int getCompareResult1(String date) throws ParseException{
//DateFormat df = new SimpleDateFormat("dd/mm/yyyy");//same error as line1
public static int getCompareResult1(String date) throws ParseException{
DateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
df.setLenient ( false );
Date d1 = df.parse(date);
Date today = new Date();
int relation;
if (d1.equals(today))
relation = 0;
else if(d1.before(today))
relation = -1;
else
relation = 1;
return relation;
}
public static int getCompareResult1(String date) throws ParseException {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
df.setLenient ( false );
DateFormat df1 = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyy", Locale.getDefault());
df1.setLenient ( false ); a
Date d1 = df.parse(date);
Date now = df1.parse(new Date().toString());
Calendar today = Calendar.getInstance(Locale.getDefault());
today.setTime(now);
Calendar compare = Calendar.getInstance(Locale.getDefault());
compare.setTime(d1);
compare.set(Calendar.HOUR_OF_DAY, 0);
compare.set(Calendar.MINUTE, 0);
compare.set(Calendar.SECOND, 0);
compare.set(Calendar.MILLISECOND, 0);
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
if (today.equals(compare)) {
return 0;
} else if(today.before(compare)) {
return -1;
} else {
return 1;
}
}
The "new Date()" line will produce a data along the lines of "Mon Apr 14 15:03:32 BST 2008", i.e.. the correct full time for now. If you are only passing "14/04/2008" to the method then it will be "Mon Apr 14 00:00:00 BST 2008".
Comparing these two will will result in a false, to result in a true then you would need to pass a date which is fully equivalent to the time at the moment today is set.
let me know if the above isn't clear.
Mike