I have a date string of the form "07-AUG-13"
.
I want to know whether the given date is within a certain period from the current date or not.
bool
isItWithin(int period){
//check if "07-AUG-13" is within a "period" months from now
}
I have a date string of the form "07-AUG-13"
.
I want to know whether the given date is within a certain period from the current date or not.
bool
isItWithin(int period){
//check if "07-AUG-13" is within a "period" months from now
}
Using the Calendar
class:
public static boolean isWithinMonths(Date startDate, Date endDate, int months) {
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startDate);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endDate);
startCalendar.add(Calendar.MONTH, months);
return startCalendar.before(endCalendar);
}
If you use Java 8 use the new time api.
LocalDate startDate = LocalDate.of(2004, Month.JANUARY, 1);
LocalDate endDate = LocalDate.of(2005, Month.JANUARY, 1);
long monthsInYear2 = ChronoUnit.MONTHS.between(startDate, endDate);
i recommend you to use [Joda Time Library] instead of JDK<8 [Joda...]
or you can try this stuff:
long diff = date1.getTime() - date2.getTime();
long monthDiff=Math.ceil((diff / (60 * 60 * 24)));