As a developer, one may come across use cases where he/she needs to add or subtract a few days from a Date object, let's take a look at a few examples.
Example 1: Add days using java.util.Calendar (Java 7 or before)
import java.util.Calendar;
public class AddDaystoDateExample {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int addDays = 10;
calendar.add(Calendar.DAY_OF_YEAR, addDays);
System.out.println("Date after adding 10 days: " + calendar.getTime());
}
}
Output:
Example 2: Add Days using java.time.LocalDateTime (Java 8 or above)
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class AddDaystoDateExample {
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
int addDays = 5;
LocalDateTime newDateTime = currentDateTime.plus(addDays, ChronoUnit.DAYS);
System.out.println("Date after adding 10 days: " + newDateTime);
}
}
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class AddDaystoDateExample {
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
int addDays = 5;
LocalDateTime newDateTime = currentDateTime.plus(addDays, ChronoUnit.DAYS);
System.out.println("Date after adding 10 days: " + newDateTime);
}
}
Output:
Example 3: Subtract days from Date
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class SubtractDaysFromDateExample {
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
int subtractDays = -5;
LocalDateTime newDateTime = currentDateTime.plus(subtractDays, ChronoUnit.DAYS);
System.out.println("Date after subtracting 5 days: " + newDateTime);
}
}
Output:
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Fix zsh: permission denied: script.sh - zsh
- How to Change Java JDK Version in IntelliJ IDE - Java
- Java Check if String is Alphanumeric using Regular Expression (RegEx) - Java
- Quickly install VS Code on macOS Sonoma/Ventura - MacOS
- C#.Net error The underlying connection was closed: An unexpected error occurred on a send - SharePoint
- Facebook Graph API Unavailable - Facebook
- Make div element draggable using jQuery - jQuery
- Android AlertDialog with Yes No and Cancel Button - Android