Java Subtract Days from Current Date10 Sept 2024 | 3 min read In Java, dealing with date and time is not much difficult task because Java provides API for date and time that makes tasks easier for the developers. In this section, we will discuss how to subtract number of days from current date and any particular day.
Using Java Calendar ClassIn the following Java program, we have used the Java Calendar class. In order to get the current date first, we have created an instance of the Calendar class, after that called the getTime() method that prints the current date and time on the console. Now let's subtract the days by calling the add() method and Calendar.DATE constant. In this method, we have passed a negative value because we are subtracting days from the current date. In the same way, we can also add days to the current date. Note that the number of days must be positive. Let's implement the above logic in a Java program. SubtractDays.java Output: Current Date = Mon Feb 14 19:15:55 GMT 2022 Updated Date = Fri Feb 11 19:15:55 GMT 2022 Instead of using the DATE field, we can also use the DAY_OF_YEAR field to get the current date. Consider the following program. SubtractDays.java Output: Current Date: Mon Feb 14 20:03:50 GMT 2022 Date before 100 days: Sat Nov 06 20:03:50 GMT 2021 Using Java LocalDate ClassJava 8 introduces a new DateTime API for manipulating date and time. The API provides many Java classes. But one of the most useful class in this API is the LocalDate class, for holding a date value. It belongs to java.time package. In the following Java program, we have used the parse() method of the LocalDate class. It is used to get an instance of the LocalDate from a string such as '2022-02-14' as a parameter. Note that the string must be a valid date-time format. Syntax: The class also provides a method minusDays() that is used to subtract number of days from the current date. The syntax of the minusDays() method is as follows: The method accepts a long value that represents the number of days subtract from the current date. Note that the number of days to subtract may be positive and negative. It returns a copy of date after subtracting the days. It throws DateTimeException if the result exceeds the supported date range. The following Java program subtract days from current date. SubtractDays.java Output: Date: 2022-02-14 New Date: 2021-12-20 The following Java program subtract days from a specified date. SubtractDays.java Output: Date: 2022-02-11 New Date: 2022-01-30 In the above program, we can also change time zone. Consider the following program for the same. SubtractDays.java Output: New Date: 2022-01-15 Next TopicJava Future Example |
CompositeName getAll() method in Java with Examples
A javax.naming.CompositeName contains the getAll() function. The entire composite object's component set is returned as an enumeration of strings using the CompositeName class. Update effects on this enumeration that are applied to this composite name are not defined. Syntax: public Enumeration getAll() Parameters: The method...
2 min read
Synchronization in Respect to Multithreading in Java
In Java multithreading, synchronization guarantees controlled access to shared resources among multiple threads to ent data inconsistency. The primary objective of synchronization is to avoid thread interference and memory consistency errors. Synchronized Method A synchronized method in Java allows only one thread to access it at any given...
10 min read
Random Flip Matrix in Java
Matrix manipulation is a fundamental aspect of computer science and programming. In Java, developers often encounter scenarios where they need to perform various operations on matrices. One interesting matrix operation is flipping, and introducing randomness to this operation can add a unique dimension to your code....
4 min read
Spirally Traversing a Matrix in Java
Traversing a matrix spirally entails moving through the elements in a circular pattern, starting from the top-left corner and moving to the right along the top row. Following every row or column traversal, the boundaries are adjusted, and the direction is switched, persisting until all elements...
10 min read
java lang exception no runnable methods
In Java, the error is basically an exception of Junit, which occurs when Junit is unable to find the runnable test methods. In the spring boot, i.e., one of the most used Java frameworks throws the no runnable methods exception where no runnable test methods...
5 min read
Program to Emulate N Dice roller in Java
In the world of programming, emulating real-world scenarios can be both a fun and educational experience. One such scenario is rolling dice, a common element in games of chance. In this section, we will explore how to create a Java program that emulates rolling N...
4 min read
Object Life Cycle in Java
Based on the idea of object-oriented programming, or OOP, Java is a flexible and popular programming language. Everything in Java is an object, and objects go through many stages in their lifetime. In order to ensure proper resource management and program functioning, Java developers need to...
4 min read
Java Program to Print All the Leaders in an Array
In the realm of programming, identifying particular elements within a dataset can be crucial for various analytical tasks. One such problem is determining the leader elements in an array. Leader in an Array A leader within a sequence is described as a component that is greater than every...
7 min read
Java Tail Recursion | What is Tail Recursion?
Tail recursion is a particular case of recursion where the recursive call is the last operation in the function. It allows some compilers or interpreters to optimize the recursive call to avoid consuming additional stack space, which can ent stack overflow errors for deeply recursive calls. Example...
5 min read
Make the String Lexicographically Larger in Java
Given a string 'str' with length N. The task is to find the greatest lexicographic string in which we can only move a single character from 'str' to any other index once. Example 1: Input: String str = "cad" int N = 3 Output: The Largest Lexicographic string is dca Explanation: The string's length...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India