Adding Days to a Date in Java

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- 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 = Gregorian​Calendar.getInstance();
    c.add(Calendar.DATE, 7);

    String s = new Simple​Date​Format("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.

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);

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.

Adding days to Date periodically in Java

You should use java.time types instead of Date and Calendar, which are considered legacy types since Java 8.

Here's an example that may suit you:

public class CountingDays {

private LocalDate date = LocalDate.now();

public static void main(String[] args) {

CountingDays countingDays = new CountingDays();

TimerTask task = new TimerTask() {

@Override
public void run() {
countingDays.date = countingDays.date.plusDays(1);
System.out.println(countingDays.date);
}
};

Timer timer = new Timer();
long delay = 0;
long intervalPeriod = 10_000;

timer.schedule(task, delay, intervalPeriod);
}
}


Related Topics



Leave a reply



Submit