
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7442 Articles for Java

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

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

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

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

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

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

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

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

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

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