tl;dr
LocalDate.now().plusDays( 30 )
Details
As others pointed out, the specific problem is an overflow of a 32-bit integer.
That code also ignores crucial issue of time zone.
The bigger problem is trying to roll-your-own date-time calculations rather than using a decent library.
java.time
The java.time classes built into Java 8 and later supplant the troublesome old date-time classes.
An Instant
is a moment on the timeline in UTC with a resolution of nanoseconds.
Instant now = Instant.now();
Apply a time zone to get a ZonedDateTime
.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
Now add your 30 days. Let java.time handle anomalies such as Daylight Saving Time (DST).
ZonedDateTime zdtLater = zdt.plusDays( 30 );
Or perhaps you meant a month. Let java.time handle the details about months having different lengths.
ZonedDateTime zdtMonthLater = zdt.plusMonths( 1 );
See this Question for info about converting between new and old types. See new methods added to the old class such as:
java.util.Date utilDate = java.util.Date.from( zdt.toInstant() );
java.util.Calendar utilCalendar = java.util.GregorianCalendar.from( ZonedDateTime );
If you want a date-only value, use LocalDate
. Same idea as above, add days or months.
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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
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.