How to add one day to a date?(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):
LocalDateTime.from(dt.toInstant()).plusDays(1);
How to add one day to a date?(Java) How to add one day to a date?(Java) Reviewed by Anonymous on July 23, 2018 Rating: 5

No comments:

Java Ternary Operator

Java Ternary Operator Java ternary operator is the only conditional operator that takes three operands. Java ternary operator is a one l...

Powered by Blogger.