×

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two ways to subract the current date and any particular day from the number of days.

A duplicate of this LocalTime is returned by the LocalTime class's minus() function with the supplied amount deducted from the date-time. 

The number of days to deduct from the current date is represented by a long value that the method takes.

The number of days to subtraction may be both positive and negative, so keep that in mind. 

After removing the days, it gives back a duplicate of the date. 

If the outcome exceeds the supported date range, it throws a DateTimeException.

  1. Java Calendar Class Utilization
  2. Using the LocalDate Class in Java

Java Calendar Class Utilization

We have incorporated the Java Calendar class into the following Java application. The getTime() function is used to output the current date and time on the console after first creating an instance of the Calendar class in order to obtain the current date. By using Calendar.DATE and the add() function, you may subtract the days.

Java program to subtract days from current date using Java Calender Class utilization

CalenderClass.java

import java . util . Calendar ;  
public class CalenderClass  
{  
public static void main ( String s [ ] )   
{  
Calendar c = Calendar . getInstance ( ) ;  
System . out . println ( " Current Date = " + c . getTime ( ) ) ;  
c . add ( Calendar . DATE , -2 ) ;  
System . out . println ( " Modified date = " + c . getTime ( ) ) ;  
}  
}  

Output

Java Subtract Days from Current Date

To obtain the current date, we may alternatively utilise the DAY OF YEAR field rather than the DATE field.

Java program to subtract days from current date using Java Calender Class

CalenderClass2.java

public class CalenderClass2
{  
public static void main ( String s [ ] )   
{  
Calendar c = Calendar . getInstance ( ) ;  
Date today = c . getTime ( ) ;  
System . out . println ( " Present Date : " + today ) ;  
c . add (Calendar.DAY_OF_YEAR , -150 ) ;  
Date d = c . getTime ( ) ;  
System . out . println ( " before 150 days date is : " + d ) ;  
}  
}  

Output

Java Subtract Days from Current Date

Using Java LocalDate Class

For managing date and time, Java 8 adds a new DateTime API. 

The API offers numerous Java classes. 

However, the LocalDate class, which stores a date value, is one of the most helpful classes in this API. The Java.time package contains it.

We have utilised the parse() function of the LocalDate class in the following Java programme. 

SubtractDays1.java

import java .time . LocalDate ;  
public class SubtractDays1   
{    
public static void main ( String args [ ] )  
{    
  
LocalDate date1 = LocalDate  .  now ( ) ;    
System . out . println ( " Present  Date " + date1 ) ;  
LocalDate newD = date1 . minusDays ( 46 ) ;  
System . out . println ( " Modified Date: "+ newD ) ;  
}  
}

Output:

Java Subtract Days from Current Date

Program to subtract days from the specified date

SubtractDays2.java

import java .time . *  ;  
public class SubtractDays2  
{    
public static void main ( String s [ ] )  
{    
LocalDate da = LocalDate . parse ( "2022-02-11" ) ;  
System . out . println (" Date : "+ da ) ;  
LocalDate newD = da . minusDays (15);  
System . out . println ( " New Date: "+newD ) ;  }  
}

Output:

Java Subtract Days from Current Date

We can also include the time zone in the above program.

SubtractDaysZone.java

public class SubtractDaysZone  
{    
public static void main (String s [ ] )  
{    
LocalDate d = LocalDate . now ( ZoneId . of("Europe/Paris")) . minusDays ( 30 ) ;  
System . out . println ( " New Date: "+ d ) ;  
}  
}  

Output

Java Subtract Days from Current Date

Java program to subtract days from the given time

SubtractDaysTime . java

public class SubtractDaysTime
{ 
public static void main ( String s [ ] ) 
{ 
LocalDate d = LocalDate . parse ( " 2018-11-14 " ) ; 
System . out . println ( " LocalDate "+ "  before subtracting days: " + d ) ; 
LocalDate value = d . minusDays ( -15 ) ;  
System . out . println ( " LocalDate after  "+ " subtracting days : " + value ); 
} 
}

Output

Java Subtract Days from Current Date

Related Topics

How to run Java Program in Command Prompt

How to run Java Program in Command Prompt In this section, we will learn how to write, save, compile, and execute or run a Java program in the Command Prompt. Note: One...

3 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

Java Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Java FileOutputStream

What is FileOutputStream?When we need raw stream data written into a file, we need to look for another option: FileOutputStream. It is used when the file's data is byte-oriented. It comes under...

4 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth...

3 minutes read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

Java LinkedList vs ArrayList

LinkedList In LinkedList, each element is a distinct entity containing an information portion and an address component, and the elements are not kept in consecutive locations. Pointers & addresses are used...

3 minutes read.

How to increment and decrement date using Java?

Before understanding how to increment and decrement the date, one must know about the Calendar class in Java. The Java calendar class offers methods for converting dates between a given moment...

3 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

ArrayList vs Vector in Java

ArrayList Vs. Vector in Java In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable...

4 minutes read.

Polygonal Number in Java

What does a Java Polygonal Number Mean? In mathematics, a polygonal number refers to a number that is expressed through dots or pebbles arranged in a regular polygonal pattern. Alphas are...

4 minutes read.