Found 7442 Articles for Java

Java program to subtract 40 days from the calendar

Alshifa Hasnain
Updated on 04-Mar-2025 19:48:35

416 Views

In this article, we will learn to subtract 40 days from the calendar in Java. Working with dates is a frequent need in programming, and there are multiple methods of doing it in Java. One of the simplest approaches is to use the Calendar class to remove days from a date. Different Approaches The following are the two to subtract 40 days from the calendar in Java − Using the Calendar Class Using the LocalDate Class Using the Calendar Class The first approach utilizes the Calendar class, which is part ... Read More

Java program to subtract year from current date

Samual Sam
Updated on 27-Aug-2024 18:47:50

2K+ Views

In this article, we will learn to subtract a year from the current date using Java. We will be using the Calender class of java.util package. Calendar class in Java is an abstract class that provides different methods to convert between a specific instant in time and a set of calendar fields such as HOUR, DAY_OF_MONTH, MONTH, YEAR, and so on. It is also used for manipulating the calender field. Steps to subtract year from current date Following are the steps to subtract year from current date − Import the Calendar class from the java.util package. ... Read More

Java Program to add year to current date using Calendar.add method

Shriansh Kumar
Updated on 16-Aug-2024 08:05:46

1K+ Views

The Calendar class of java.util package provides a method with the name add(). This method accepts current date and amount of time as parameter values. If the given amount of time is positive, it will add it to the current date, and in the case of negative time, it will subtract it. In this article, we will see a few Java programs to add year to the current date using the Calendar.add() method. Example Scenario: Input 1: current_date = Thu Nov 22 18:19:06 UTC 2018 Input 2: year_to_add = 12 Output: new_date = Thu Nov 22 18:19:06 UTC 2030 ... Read More

Add week to current date using Calendar.add() method in Java

Samual Sam
Updated on 27-Jun-2020 13:34:17

2K+ Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current date and timeCalendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us increment the weeks using the calendar.add() method and Calendar.WEEK_OF_YEAR constant.calendar.add(Calendar.WEEK_OF_YEAR, 2);The following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Adding 2 weeks       calendar.add(Calendar.WEEK_OF_YEAR, 2);       System.out.println("Updated Date = " + calendar.getTime());   ... Read More

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

karthikeya Boyini
Updated on 27-Jun-2020 13:35:46

666 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current date and timeCalendar 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 decrementingcalendar.add(Calendar.SECOND, -20);The following is an exampleExample Live Demoimport 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, ... Read More

Java Program to add days to current date using Java Calendar

Samual Sam
Updated on 27-Jun-2020 13:39:54

1K+ Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us add days using the add() method and Calendar.DATE constantcalendar.add(Calendar.DATE, 2);The following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Incrementing Days by 2       calendar.add(Calendar.DATE, 2);       System.out.println("Updated Date = " + calendar.getTime());   } }OutputCurrent Date = ... Read More

Decrement a Month using the Calendar Class in Java

karthikeya Boyini
Updated on 27-Jun-2020 13:41:05

230 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us decrement the month using the add() method and Calendar.MONTH constant. Set a negative value here since we are decrementingcalendar.add(Calendar.MONTH, -2);The following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Decrementing Month by 2       calendar.add(Calendar.MONTH, -2);       System.out.println("Updated ... Read More

Increment a Month using the Calendar Class in Java

Samual Sam
Updated on 27-Jun-2020 13:24:02

491 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us increment the month using the add() method and Calendar.MONTH constant −calendar.add(Calendar.MONTH, 2);The following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Incrementing Month by 2       calendar.add(Calendar.MONTH, 2);       System.out.println("Updated Date (+2 Months) = " + calendar.getTime());   ... Read More

Java Program to decrement a Date using the Calendar Class

karthikeya Boyini
Updated on 27-Jun-2020 13:25:41

183 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us decrement the date using the add() method and Calendar.DATE constant. Set a negative value since we are decrementing the datecalendar.add(Calendar.DATE, -3);The following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Decrementing date by 3       calendar.add(Calendar.DATE, -3);       ... Read More

Increment a Date using the Java Calendar Class

Samual Sam
Updated on 27-Jun-2020 13:26:13

318 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us increment the date using the add() method and Calendar.DATE constantcalendar.add(Calendar.DATE, 2);The following is the complete exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Incrementing date by 2       calendar.add(Calendar.DATE, 2);       System.out.println("Updated Date = " + calendar.getTime());    } }OutputCurrent ... Read More

Advertisements