×

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

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Perfect Number Program in Java

Perfect Number Program in Java A perfect number is a number whose sum of all the factors, excluding the number itself, is equal to the number. For example, 28 is a...

4 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

Java Variable Declaration

In this article, you will be acknowledged about java variable declaration. You will be able to learn and interpret about declaring a variable in Java. Variable in Java Variables are necessary for...

6 minutes read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Java Integer remainder Unsigned() method

The remainderUnsigned() method of Java Integer class returns the unsigned remainder by dividing the first and second argument. Syntax public static int remainderUnsigned(int dividend, int divisor)  Parameters The ‘dividend’ and ‘divisor’ represents the value...

1 minute read.

Implementing Queue Using Array in Java

We can implement queue by using array in Java. The queue is a linear data structure, and the array is one of the simplest data structures. Before implementing a queue...

4 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.

Java Viva Questions and Answers

Question: Explain Java programming language. Answer: It is a high-level programming language. This programming language is based on the principles of object-oriented programming. Large-scale applications can be developed by using this...

16 minutes read.

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Types of Garbage Collector in Java

Garbage collection is a Java feature that offers automatic memory management. The JVM is in charge of it. The programmer does not have to handle object creation and deallocation. We...

3 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.

Java Integer min() method

The min()  method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min.  Syntax public static int min(int a, int b) Parameters The parameters ‘a’...

2 minutes read.