Dear sir
In my project i have to show amount of all the months i.e if user enter first due date and expiry date then it calculate the amount of all the months which are between first due date and expiry date in reports. in Jsp how a month can be added in a table date value.
if my filelds are :

java.sql.Date duedate=rs.getDate("firstrentduedate");
		 
java.sql.Date expirydate =rs.getDate("expiryofrent");
while(duedate .after(expirydate))
		  {
duedate=duedate+'addmonth'
}

plz help

Dani AI

Generated

As suggested, month arithmetic can be done with a date API; in modern Java the cleanest choice is java.time (Java 8+). Also agree with : this sort of logic belongs in a servlet/bean/service, not as scriptlets inside a JSP. The approach below generates a list of month entries (YearMonth) between the first due date and the expiry date, then maps each month to the appropriate due-date inside that month.

// assuming java.sql.Date firstRentDueDate and expiryOfRent (from ResultSet)
LocalDate start = firstRentDueDate.toLocalDate();
LocalDate end   = expiryOfRent.toLocalDate();

YearMonth ymStart = YearMonth.from(start);
YearMonth ymEnd   = YearMonth.from(end);

List<YearMonth> months = new ArrayList<>();
for (YearMonth ym = ymStart; !ym.isAfter(ymEnd); ym = ym.plusMonths(1)) {
    months.add(ym);
}

// derive a monthly due date that keeps the original day-of-month when possible
int originalDay = start.getDayOfMonth();
for (YearMonth ym : months) {
    int day = Math.min(originalDay, ym.lengthOfMonth());
    LocalDate monthlyDue = ym.atDay(day);
    // compute/store the amount for monthlyDue (use BigDecimal for money)
}

Notes and troubleshooting tips:

  • Handle nulls from ResultSet.getDate and validate that start is not after end. The loop above is inclusive of both start and end months; change the loop condition if exclusivity is required.
  • End-of-month issues (Jan 31 -> Feb) are handled by taking Math.min(day, lengthOfMonth()). Test with leap years and month boundaries.
  • Use java.time for clarity and immutability; if stuck on pre-Java-8 runtimes, the Calendar approach mentioned by is the legacy alternative.
  • Keep presentation out of JSP: compute the month/amount list in a controller, set it as a request attribute, and render with JSTL/EL. Add unit tests that cover month boundaries and business rules for partial months.

Recommended Answers

All 4 Replies

Use Calendar and its add method.

but don't do it in a JSP.

Use Calendar and its add method.

Can You specify some code for it

Thanks

Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(field, amount);
c.getTime();

Read the API docs, they contain more than enough information for this sort of thing.

As mentioned, however, you should not be doing this in a JSP. Scriptlets have no place anymore in a JSP. They are only still allowed so as not to break old JSP (i.e. backwards compatability) but you should not be creating any new scriptlets. Use a Bean.

commented: Nice example as always +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.