Java Program to subtract week from current date

Java 8Object Oriented ProgrammingProgramming

Firstly, you need to import the following package for Calendar class in Java.

import java.util.Calendar;

Create a Calendar object and display the current date and time.

Calendar calendar = Calendar.getInstance();
System.out.println("Current Date and Time = " + calendar.getTime());

Now, let us decrement the weeks using the calendar.add() method and Calendar.WEEK_OF_YEAR constant. Set a negative value since we are decrementing here.

calendar.add(Calendar.WEEK_OF_YEAR, -2);

Example

 Live Demo

import java.util.Calendar;
public class Demo {
   public static void main(String[] args) {
      Calendar calendar = Calendar.getInstance();
      System.out.println("Current Date = " + calendar.getTime());
      // Subtract 2 weeks
      calendar.add(Calendar.WEEK_OF_YEAR, -2);
      System.out.println("Updated Date = " + calendar.getTime());
   }
}

Output

Current Date = Thu Nov 22 18:14:20 UTC 2018
Updated Date = Thu Nov 08 18:14:20 UTC 2018
raja
Published on 06-Dec-2018 16:53:08
Advertisements