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.
+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
2009-04-05 10:02:01
+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
2009-04-05 10:18:31
Why use Joda for something as simple as adding 3 months to a date?
javashlook
2009-04-05 21:31:54
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
2009-04-06 07:35:45
A:
If you need to work with date arithmetic JODA works better, as Calendar likes timestamps.
Thorbjørn Ravn Andersen
2009-04-05 18:07:46