Syntax error Subtract minutes from current time using Calendar.add() method in Java

Subtract minutes from current time using Calendar.add() method in Java



Import the following package for Calendar class in Java.

import java.util.Calendar;

Firstly, 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 minutes using the calendar.add() method and Calendar.MINUTE constant. Set a negative value since you want to decrease the minutes.

calendar.add(Calendar.MINUTE, -15);

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 15 minutes from current date
      calendar.add(Calendar.MINUTE, -15);
      System.out.println("Updated Date = " + calendar.getTime());
   }
}

Output

Current Date = Thu Nov 22 16:26:19 UTC 2018
Updated Date = Thu Nov 22 16:11:19 UTC 2018
Updated on: 2020-06-25T12:16:40+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements