Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Subtract seconds 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 seconds using the calendar.add() method and Calendar.SECOND constant. Set a negative value since we are decrementing
calendar.add(Calendar.SECOND, -20);
The following is an example
Example
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 20 seconds from current date
calendar.add(Calendar.SECOND, -20);
System.out.println("Updated Date = " + calendar.getTime());
}
}
Output
Current Date = Thu Nov 22 18:05:36 UTC 2018 Updated Date = Thu Nov 22 18:05:16 UTC 2018
Advertisements
