views:

1244

answers:

4

My program is about generating (producing) a Kurosawa and making the customers produce it. Every time we generate a Kurosawa, we have to print its id, its production date and expiration date, which is 3 months from the production date. My problem is: How can I calculate the date after 3 months? Thank you.

+2  A: 

I believe that the Java Calendar Library should help you.

Yuval F
+8  A: 

Use the built-in Java Calendar API.

Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 3);

Refer to the API for exactly how to print out the date, in the format you are looking for.

Yuval A
+7  A: 

You could also use the much more powerful and easier to use Joda Time Library:

DateMidnight productionDate = new DateMidnight();
DateMidnight expirationDate = productionDate.plusMonths(3);
System.out.println(expirationDate.toString("dd.MM.yyyy"));

Joda Time has many advantages over the built-in Java Calendar API.

Ludwig Wensauer
Why use Joda for something as simple as adding 3 months to a date?
javashlook
You are right but when you add 3 months to a java.util.Date/Calendar you manipulate the same instance which could be error-prone. The Joda-Time objects are immutable. And I think you normally do something more than with date's than just add 3 months. I hope that Java7 will include Joda-Time(JSR310)
Ludwig Wensauer
A: 

If you need to work with date arithmetic JODA works better, as Calendar likes timestamps.

Thorbjørn Ravn Andersen