Here inactiveDate is SQL Date.
Here is the simple logic to get only Date and do a comparison without worrying about milliseconds.
Date inDate = abcTO.getInDate();
String inDateString = inDate.toString();
Date currentSysDate = new Date(System.currentTimeMillis());
String currentSysDateString = currentSysDate.toString();
int dateValue = inDate.compareTo(currentSysDate);
if(0 > dateValue && (!inDateString.equals(currentSysDateString))) {
inDateString.equals(currentSysDateString)
The above line is used purely to compare current date with out taking milliseconds into account.
Here is a code sample for it
Date Subtraction
public static Double compareDates(Date date1, Date date2) {
double difYears = 0.0;
if(date1!= null & date2!= null) {
long difMillSecs = date1.getTime() - date2.getTime();
double difSecs = (double)difMillSecs / 1000.0;
double difMins = difSecs / 60.0;
double difHours = difMins / 60.0;
double difdays = difHours/ 24.0;
difYears = difdays/365.0;
}
System.out.println("The Date difference in no. of days is "+difdays);
System.out.println("The Date difference in no. of years is "+difYears);
return difYears ;
}
Date parsing:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = sdf.parse(“15/35/08”);
As you can see in the above lines of code we are parsing the date with the parse method with an invalid input. For MM we are passing a value which is greater than 12 and date we are passing 35. For year which is in yyyy format we are passing just last 2 digits (08) instead of 2008.
Parse method just see if the format is correct rather than the values.
So to do a thorough testing we do some custom date validation
try{
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = sdf.parse("15/25/2008");
System.out.println("Date: " + date);
Calendar cd = Calendar.getInstance();
cd.setTime(date);
int year = cd.get(Calendar.YEAR);
int month = cd.get(Calendar.MONTH);
int day = cd.get(Calendar.DAY_OF_MONTH);
System.out.println("Year: " + year + " Month: " + (month+1) + " Day: " + day);
if ((date == null) or year < 1000 or year > 9999 or month < 0 or month > 12 or day < 0 or day > 31) {
System.out.println("Wrong format");
}
}catch(Exception ex) {
System.out.println("Date Exception: " + ex);
}
Output:Date: Wed Mar 25 00:00:00 IST 2009
Year: 2009 Month: 3 Day: 25
What we understand from the above output is if the month is given more than 12 as input then it is counted as next year months. Same case for days as well. If it is given more than 31 then it is counted as next month days.
In the above example since month is given as 15, extra 3 months is counted as next years months.
Input:Date date = sdf.parse("05/35/2008");
Output:Date: Wed Jun 04 00:00:00 IST 2008
Year: 2008 Month: 6 Day: 4
In the above example 4 extra days in the month of may (5th month) are added to the next month (6th) as 4th day.
Input:Date date = sdf.parse("05/15/08");
Output:Date: Tue May 15 00:00:00 IST 8
Year: 8 Month: 5 Day: 15
Wrong format
We are getting the “Wrong format“ error message since the year format is specified as “yyyy” and only 2 no are given (08) instead of 2008.
Hence if condition can be restricted only to years without having to check for days and months.
if ((date == null) or year < 1000 or year > 9999 )
SimpleDateFormat’s parse method will fail only if the input format is completely different from what’s mentioned as constructor parameter.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = sdf.parse("15-25-2008");
Above code will fail as the format is completely different from that of what’s mentioned in the constructor format.