Subtract Week from Current Date in Java



In this article, we will learn to subtract the week from the current date using Java. This is useful when you need to manipulate dates, such as calculating dates from previous weeks for scheduling or tracking purposes. We'll use two approaches: using the Calendar class and the LocalDate class, showcasing how to easily adjust dates.

Problem Statement

Write a program in Java to subtract a specified number of weeks from the current date and display the updated date ?

Input

Run the program

Output

Current Date = Mon Nov 04 09:41:18 IST 2024
Updated Date = Mon Oct 21 09:41:18 IST 2024

Different approaches

Following are the different approaches to subtract the week from the current date ?

Using Calendar class

Following are the steps to subtract the week from the current date ?

  • First, we will import the Calendar class from java.util package.
  • We will initialize the public class Demo.
  • To get the current date we will use Calendar.getInstance().
  • We will use the calendar.add() method with Calendar.WEEK_OF_YEAR and a negative integer to subtract weeks.
  • Finally, we will print the updated date on the console.

Example

Below is the Java program to subtract the week from the current date using the Calendar class ?

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

Using LocalDate class

Following are the steps to subtract the week from the current date using the LocalDate class ?

  • First, we will import the LocalDate from java.time package.
  • We will create a public class named Demo add the main method and use LocalDate.now() to retrieve the current date and store it in a variable named currentDate.
  • Print the currentDate to the console to show today's date.
  • Call currentDate.minusWeeks(2) to subtract 2 weeks from the current date, and store the result in updatedDate.
  • Print the updatedDate to the console to show the date from two weeks ago.

Example

Below is the Java program to subtract the week from the current date using the LocalDate class ?

import java.time.LocalDate;
public class Demo {
    public static void main(String[] args) {
        // Get the current date
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current Date = " + currentDate);

        // Subtract 2 weeks
        LocalDate updatedDate = currentDate.minusWeeks(2);
        System.out.println("Updated Date = " + updatedDate);
    }
}

Output

Current Date = 2024-11-04
Updated Date = 2024-10-21
Updated on: 2024-11-04T18:44:16+05:30

783 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements