Update for the modern age, using the java.time classes that supplant the troublesome old legacy date-time classes including Calendar
.
Time zone
Define your time zone. 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(!). I am guessing you meant India time by IST
, but perhaps Irish Standard Time or other?
final ZoneId z = ZoneId.of ( "Asia/Kolkata" );
The ZonedDateTime
class represents a moment on the timeline adjusted into a particular time zone.
final ZonedDateTime zdt = ZonedDateTime.of ( 2013, 5, 31, 18, 33, 0, 0, z );
plusMonths
| minusMonths
final ZonedDateTime zdtMonthPlus = zdt.plusMonths ( 1 );
final ZonedDateTime zdtMonthMinus = zdtMonthPlus.minusMonths ( 1 );
Dump to console.
System.out.println ( "zdt.toString(): " + zdt );
System.out.println ( "zdtMonthPlus.toString(): " + zdtMonthPlus );
System.out.println ( "zdtMonthMinus.toString(): " + zdtMonthMinus );
We see the same behavior as with the legacy Calendar
class. When adding a month from May 31 there is no June 31st so it falls back to the last day of the month, June 30. When subtracting from June 30, the class tries to match the same day-of-month, so it uses May 30.
zdt.toString(): 2013-05-31T18:33+05:30[Asia/Kolkata]
zdtMonthPlus.toString(): 2013-06-30T18:33+05:30[Asia/Kolkata]
zdtMonthMinus.toString(): 2013-05-30T18:33+05:30[Asia/Kolkata]
plus( Duration)
| minus( Duration )
If you want another behavior, use another approach to your code. For example, if by "month" you really mean “30 days of generic 24-hour days (ignoring anomalies such as Daylight Saving Time)”, then add/subtract durations of 30 days.
Duration thirtyDays = Duration.ofDays( 30 ); // 30 days of generic 24-hour length. Ignoring anomalies such as DST. Ignoring calendar months.
final ZonedDateTime zdtMonthPlusDuration = zdt.plus ( thirtyDays );
final ZonedDateTime zdtMonthMinusDuration = zdtMonthPlusDuration.minus ( thirtyDays );
thirtyDays.toString(): PT720H
zdtMonthPlusDuration.toString(): 2013-06-30T18:33+05:30[Asia/Kolkata]
zdtMonthMinusDuration.toString(): 2013-05-31T18:33+05:30[Asia/Kolkata]
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?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
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.