9

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154

6 Answers6

15

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 Adam
  • 161,610
  • 92
  • 305
  • 395
9

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
  • 1,885
  • 3
  • 32
  • 43
  • Why use Joda for something as simple as adding 3 months to a date? – javashlook Apr 05 '09 at 21:31
  • 1
    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 Apr 06 '09 at 07:35
3

tl;dr

LocalDate.now()            // Determine today’s date.
         .plusMonths( 3 )  // Add three months.

java.time

The modern approach uses java.time classes.

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

Add three months, letting java.time do the math.

LocalDate threeMonthsLater = today.plusMonths( 3 ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2

I believe that the Java Calendar Library should help you.

Yuval F
  • 20,565
  • 5
  • 44
  • 69
0

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

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

/The java class Calendar is abstract. So you need to use the GregorianCalendar class./

java.util.GregorianCalendar gC = new java.util.GregorianCalendar();

java.util.Date yourDate = new java.util.Date();
gC.setTime(yourDate);

/Add 3 months/

gC.add(java.util.Calendar.MONTH_OF_YEAR, 3);

/to go back 1 week/

gC.add(java.util.Calendar.WEEK_OF_YEAR, -1);
hcv
  • 1
  • 2
    The [getInstance](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getInstance%28%29) methods of Calendar are static so you can use them. – KSev Apr 16 '12 at 20:53
  • 1
    And are preferred, because getInstance() is a static factory method (see Effective Java, Joshua Bloch) which will try to determine the best time for your locale. – Visionary Software Solutions Nov 28 '12 at 10:18