views:

76

answers:

2

Hey guys, I know this is a very simple question but I am new to Java so please bear with me. I have create a program that can calculate the time a book is due to come back after being issued! So please help me out.

Here is the piece of code that I have:

  public void loan(int numOfDays)   {

  if( onLoan == true)   {

System.out.println("Already on Loan, Return Date : "+Calendar.DATE );
}
else  {
due.set( numOfDays, Calendar.DATE );
System.out.println("Available");
onLoan = true;

}  }

// Mark the book as having been returned

I need to replace that Calendar.DATE to get my calculations right. I need to calculate the date passed by the TEST file and calculate the date it will be returned. This project needs to be submitted TOMORROW, so please please help me out!

+2  A: 

You're close to the solution. You're looking for:

YOUR_CALENDAR_INSTANCE.add(numOfDays, Calendar.DAY_OF_MONTH);

[http://java.sun.com/javase/6/docs/api/java/util/GregorianCalendar.html#add(int, int)][1]

[1]: http://java.sun.com/javase/6/docs/api/java/util/GregorianCalendar.html#add(int, int)

Tansir1
+1  A: 

The standard java Date API is a pain. Although the previous answer is correct, and might be sufficient for you I suggest taking a look at jodaTime if you need to work with dates. Many of the headaches are solved in there.

Albert