How to add one day to a date?
Given a Date dt
you have several possibilities:
Solution 1: You can use the Calendar
class for that:
Date dt = new Date();
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.add(Calendar.DATE, 1);
dt = c.getTime();
Solution 2: You should seriously consider using the Joda-Time library, because of the various shortcomings of the Date
class. With Joda-Time you can do the following:
Date dt = new Date();
DateTime dtOrg = new DateTime(dt);
DateTime dtPlusOne = dtOrg.plusDays(1);
Solution 3: With Java 8 you can also use the new JSR 310 API (which is inspired by Joda-Time):
Date dt = new Date();
LocalDateTime.from(dt.toInstant()).plusDays(1);
Java 8 adding days with no time segments
Calendar.getInstance() returns a Calendar instance that contains time components, so you need to clear the time components from the Calendar instance.
Here is correct code:
// today
Calendar calendar = Calendar.getInstance();
// Add 1 day -> tomorrow
calendar.add(Calendar.DAY_OF_YEAR, 1);
// Clear time components
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
// Util date tomorrow
Date tomorrow = calendar.getTime();
Notes: you are not utilizing Java 8 Date/Time API. The below is the way if you use Java 8+
// java 8+ Date/time API
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plusDays(1);
// Java8+ tomorrow to Util tomorrow if you want
java.util.Date tomorrowUtilDate = java.sql.Date.valueOf(tomorrow);
java- How to add 7 days to a default date?
LocalDate is perfect for this job:
LocalDate.now().plusDays(7);
You can get your string representation with
.format(DateTimeFormatter.ISO_DATE);
If you're not able to use Java 8, then you have a few options:
Use the ThreeTen-Backport, which backports most functionality of the Java 8 JSR-310 API, normally available in the
java.time
package. See here for details. This package is available in Maven Central.You can also use Joda Time. The peculiar thing is that these two projects have almost the same layout of their websites.
If you're otherwise not able to use ThreeTen-Backport or Joda Time, you can use this:
Calendar c = GregorianCalendar.getInstance();
c.add(Calendar.DATE, 7);
String s = new SimpleDateFormat("yyyy-MM-dd")
.format(c.getTime());
Warning
Many things are wrong with the old Date and Time API, see here. Use this only if you have no other option.
Add 30 days to Date in java
This is because 30 * 1000 * 60 * 60 * 24
overflows Integer.MAX_VALUE
, while 20 * 1000 * 60 * 60 * 24
does not.
How to add days to a date in Java
Make use of Calendar#add()
. Here's a kickoff example.
Calendar dom = Calendar.getInstance();
dom.clear();
dom.set(y, m, d); // Note: month is zero based! Subtract with 1 if needed.
Calendar expire = (Calendar) dom.clone();
expire.add(Calendar.DATE, 100);
If you want more flexibility and less verbose code, I'd recommend JodaTime though.
DateTime dom = new DateTime(y, m, d, 0, 0, 0, 0);
DateTime expire = dom.plusDays(100);
How add 30 days in current date?
You need to use
c1.add(Calendar.DAY_OF_YEAR, 30);
instead of
c1.add(Calendar.DATE, 30);
Try this
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd");
Calendar c1 = Calendar.getInstance();
String currentDate = df.format(date);// get current date here
// now add 30 day in Calendar instance
c1.add(Calendar.DAY_OF_YEAR, 30);
df = new SimpleDateFormat("yyyy-MM-dd");
Date resultDate = c1.getTime();
String dueDate = df.format(resultDate);
// print the result
Utils.printLog("DATE_DATE :-> "+currentDate);
Utils.printLog("DUE_DATE :-> "+dueDate);
OUTPUT
2019-06-04 14:43:02.438 E/XXX_XXXX: DATE_DATE :-> 2019-06-04
2019-06-04 14:43:02.438 E/XXX_XXXX: DUE_DATE :-> 2019-07-04
Related Topics
Filling Combobox from Database by Using Hibernate in Java
Java JSON Serialization - Best Practice
Good Examples Using Java.Util.Logging
Java 8 Streams - Collect VS Reduce
Java.Lang.Unsupportedclassversionerror Unsupported Major.Minor Version 51.0
Calling Static Method from Another Java Class
Gradle - Getting the Latest Release Version of a Dependency
Parsing PDF Files (Especially with Tables) with PDFbox
How to Save Parsed and Changed Dom Document in Xml File
Auto Adjust the Height of Rows in a Jtable
How to Escape Reserved Words in Hibernate's Hql
How to Handle It with Scanner (Java)
Does the Preparedstatement Avoid SQL Injection
Differencebetween Cascadetype.Remove and Orphanremoval in JPA
Multiple Runwith Statements in Junit