How to Increment a Date by One Day 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);

Add one day to given date in Java and return in specific date format

You're using the old calendar/date API. This API is quite bad (it does weird things and does not accurately model how dates actually work).

It has been replaced with the new java.time API. I strongly suggest you use that instead. If you're on java7 or below, you can use the 'JSR310-backport' library to your dependency list to use this API. (JSR310 is the name for this addition to java).

In java.time, you'd do:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Test {
public static void main(String[] args) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse("2018-08-31", fmt);
System.out.println(fmt.format(date.plusDays(1)));

// yyyy-MM-dd so happens to be the default for LocalDate, so...
// we can make it a lot simpler:
date = LocalDate.parse("2018-08-31");
System.out.println(date.plusDays(1));
}
}

Increment existing date by 1 day

Calendar c = Calendar.getInstance();
c.setTime(yourdate);
c.add(Calendar.DATE, 1);
Date newDate = c.getTime();

Increment Date String by 1 Day

You should use new SimpleDateFormat("dd.MM.yyyy");

'mm' means minutes, 'MM' is months.

java Date object- how to increment day of week

You need to use a Calendar.

The java.util.Calendar class is an abstract encapsulation of the Date object.

Calendar provides getter and setter for the date fields.


Updated to an example of incrementing the day of the week as requested:

        Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK,(calendar.get(Calendar.DAY_OF_WEEK)+1));
//alternative:
//calendar.add(Calendar.DAY_OF_WEEK, 1);
Date newDate = calendar.getTime();


Update: Note the java 8+ implementation using java.time

Calendar and Date have not been deprecated, you can still mix and match.

However if you want to handle time zones properly or want to do more localisation (when do you not?) then you are better off using java.time.

How can i increment my date by 1

The code should be working fine as long as dt and days are correct. This gave me 12-18-2014:

String dt = "12-17-2014"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1); // number of days to add
dt = sdf.format(c.getTime());


Related Topics



Leave a reply



Submit