Add more than 30 days using the add () method in Java

I'm not quite sure which field to use when adding more than 30 days to a Java Calendar object. Is there a difference between Calendar.DAY_OF_MONTH and Calendar.DAY_OF_YEAR ?

Example:

 GregorianCalendar d = new GregorianCalendar(); d.add(Calendar.DAY_OF_YEAR, 90); 

against

 GregorianCalendar d = new GregorianCalendar(); d.add(Calendar.DAY_OF_MONTH, 90); 

Thank.

+14
java calendar
Mar 24 '10 at 7:52
source share
2 answers

I do not think it matters when you call add. The difference is important when you call getters.

Both methods work fine, right? More than 30 days, as well as negative quantities.

(admittedly complex) the source for GregorianCalendar # add has this section:

  case DAY_OF_MONTH: // synonym of DATE case DAY_OF_YEAR: case DAY_OF_WEEK: break; 
+17
Mar 24 '10 at 7:54
source share

TL; DR

 LocalDate.now( ZoneId.of( "America/Montreal" ) ) .plusDays( 30 ) 

More details

It is much easier now with the modern java.time classes, which replace the old Calendar and Date classes.

LocalDate

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

The time zone is critical for determining the date. At any given moment, the date changes around the world by zone. For example, a few minutes after midnight in Paris, France is a new day, still "yesterday" in Montreal Quebec .

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

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

You can add to it a few days.

 LocalDate later = today.plusDays( 30 ); 

Period

You can imagine a time span with the Period class.

 Period thirtyDays = Period.ofDays( 30 ); 

You can do the math by date by calling the plus or minus methods.

 LocalDate later = today.plus( thirtyDays ); 



About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

Where to get java.time classes?

  • Java SE 8 and SE 9 and later
    • Built in.
    • Part of the standard Java API with integrated implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Most of the functionality of java.time has been ported to Java 6 and 7 in ThreeTen-Backport .
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) specifically for Android.
    • See How to use ThreeTenABP ....

The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

0
Mar 25 '17 at 10:18
source share



All Articles