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,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- How to integrate Salesforce CRM Sales and Service with Microsoft Teams - Teams
- JavaScript: Count Words in a String - JavaScript
- Java Decompiler Eclipse Plugin - Eclipse
- [Python] Fix: TypeError: NoneType object is not subscriptable - Python
- Calculate Volume of Cube - C-Program
- Parsing CSV file using Java code example (Comma Separated File) - Java
- How to Create Awesome Quizzes using Windows 365 Forms - Windows
- Shortcut: Cut (or Delete) current line in Visual Studio Code (VSCode) - HowTos