I have a database field where date is stored as DD/MM/YYYY format. Now what I want to do is to see if today date = stored date – 1. Basically if today is the day before the end date. Based on that I will do some action…
example of format:
10/02/2012
There is formatting issue here because am using this:
Date todayDate;
And the stored date is VARCHAR2 in DB. Am assuming I would have to convert the retrieved date to Date regular format, or something like that. Help would be appreciated.
Update:
currentEndDate = rset.getString("ENDDATE");
endDate = (Date)formatter.parse(currentEndDate);
/*Check if end date = today date - 1*/
Calendar cal = Calendar.getInstance();
//set to end date
cal.setTime(endDate);
//end date - 1
cal.add(Calendar.DATE, -1);
Date today;
Now this is what I have..I am trying here to get the end date -1 so I did that using add() function. Now How can I check if end-date – 1 == today date or not? because when I tried :
if(today.equals(cal.add(Calendar.date, -1)))
it said void return from the add function.
Thanks,
You need to turn the String into a date. You can do this in your query or after you get the string result:
Option 1: turn it into a Date type in your query:
Option 2: turn it into a Date type after you get the string:
Then you can see if the current time (
new Date()) is between the retrieved date and a day before that. If you want to handle all corner cases including leap seconds, you’ll have to use aCalendar, but if you’re okay with leaving out leap seconds then it’ll be fine to just subtract 24*60*60*1000 milliseconds from your db date.You can’t directly compare a
Dateand aCalendaras per the API documentation:Also,
Calendar‘sequals()won’t work:Simple solution: use the
Calendar‘sgetTime()method, which returns aDateobject which will readily compare with other dates usingequals(),after(), andbefore().Or in code:
You could also instantiate another
Calendarand use itssetTime(), but that seems less efficient.