0

i am designing a page where the user enters the start date for a uni session say
04/02/2008 in dd/mm/yyyy format and then the user selects the duration for the session in weeks, say 22.
our system needs to calculate the end date for the session by adding 22 weeks to the start date...i can do this in real life by looking up a calender that shows week of the year....how do we do it programming.
we are using java/jsp, any suggestions please.

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by ~s.o.s~
0

Use the SimpleDateFormat class to parse the string into a Date object. Feed that Date object to the instance of the Calendar object and use it's function roll() to do the dirty work for you. Something along the lines of:

public class DateHelper {
    public static void main(String args[]) throws ParseException {
        String dateStr = "13/01/2008";
        SimpleDateFormat sdf = new SimpleDateFormat("DD/mm/yyyy");
        Date dt = sdf.parse(dateStr);
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        cal.roll(Calendar.WEEK_OF_YEAR, 22);    //add 22 weeks
        Date newDt = cal.getTime();
        System.out.println("Training from " + dt + " to " + newDt);
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.