week in year....calculation

Reply

Join Date: Feb 2006
Posts: 24
Reputation: nemo is an unknown quantity at this point 
Solved Threads: 0
nemo's Avatar
nemo nemo is offline Offline
Newbie Poster

week in year....calculation

 
0
  #1
Feb 19th, 2008
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.
Mathematics possesses not only truth, but supreme beauty …
Bertrand Russell
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: week in year....calculation

 
0
  #2
Feb 20th, 2008
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:
  1. public class DateHelper {
  2. public static void main(String args[]) throws ParseException {
  3. String dateStr = "13/01/2008";
  4. SimpleDateFormat sdf = new SimpleDateFormat("DD/mm/yyyy");
  5. Date dt = sdf.parse(dateStr);
  6. Calendar cal = Calendar.getInstance();
  7. cal.setTime(dt);
  8. cal.roll(Calendar.WEEK_OF_YEAR, 22); //add 22 weeks
  9. Date newDt = cal.getTime();
  10. System.out.println("Training from " + dt + " to " + newDt);
  11. }
  12. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC