This question already has an answer here:
I want to add one day to a particular date. How can I do that?
Date dt = new Date();
Now I want to add one day to this date.
This question already has an answer here:
I want to add one day to a particular date. How can I do that?
Date dt = new Date();
Now I want to add one day to this date.
Java 8 Time API:
Java 1.8 version has nice update for data time API.
Here is snippet of code:
Output:
For more info see Java documentations to this classes:
I prefer joda for date and time arithmetics because it is much better readable:
Or
you can use this method after import org.apache.commons.lang.time.DateUtils:
best thing to use:
Similarly, you can add MONTHS, DAYS, MINUTES etc
tl;dr
java.time
Best to avoid the
java.util.Date
class altogether. But if you must do so, you can convert between the troublesome old legacy date-time classes and the modern java.time classes. Look to new methods added to the old classes.Instant
The
Instant
class, is close to being equivalent toDate
, both being a moment on the timeline.Instant
resolves to nanoseconds, whileDate
is milliseconds.You could add a day to this, but keep in mind this in UTC. So you will not be accounting for anomalies such as Daylight Saving Time. Specify the unit of time with the
ChronoUnit
class.ZonedDateTime
If you want to be savvy with time zones, specify a
ZoneId
to get aZonedDateTime
. Specify a proper time zone name in the format ofcontinent/region
, such asAmerica/Montreal
,Africa/Casablanca
, orPacific/Auckland
. Never use the 3-4 letter abbreviation such asEST
orIST
as they are not true time zones, not standardized, and not even unique(!).You can also represent your span-of-time to be added, the one day, as a
Period
.You may want the first moment of that next day. Do not assume the day starts at 00:00:00. Anomalies such as Daylight Saving Time (DST) mean the day may start at another time, such as 01:00:00. Let java.time determine the first moment of the day on that date in that zone.
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.Update: The Joda-Time library is now in maintenance mode. The team advises migration to the java.time classes. I am leaving this section intact for history.
Joda-Time
The Joda-Time 2.3 library makes this kind of date-time work much easier. The java.util.Date class bundled with Java is notoriously troublesome, and should be avoided.
Here is some example code.
Your java.util.Date is converted to a Joda-Time DateTime object. Unlike a j.u.Date, a DateTime truly knows its assigned time zone. Time zone is crucial as adding a day to get the same wall-clock time tomorrow might mean making adjustments such as for a 23-hour or 25-hour day in the case of Daylight Saving Time (DST) here in the United States. If you specify the time zone, Joda-Time can make that kind of adjustment. After adding a day, we convert the DateTime object back into a java.util.Date object.
Dump to console…
When run…