- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 34494 Articles for Programming
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
582 Views
In the world of Java programming, there are a few scenarios where we are required to deal with the date and time such as while developing a calendar application, attendance management system in Java and checking age of two persons. Also, the date is a way of keeping track of our time as it is an integral part of our daily lives. Therefore, Java provides classes like Date and LocalDate to work with date and time. And, to compare and check if a date is after another or not, it provides a few useful built-in methods like 'compareTo()' and 'after()'. ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
1K+ Views
Firstly, you need to import the following package for Calendar class in Javaimport java.util.Calendar;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 subtract 1 year using the calendar.add() method and Calendar.YEAR constant. Set a negative value since we are decrementing here −calendar.add(Calendar.YEAR, -1);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 1 year from the ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
641 Views
Firstly, you need to import the following package for Calendar class in Javaimport java.util.Calendar;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 add 3 months using the calendar.add() method and Calendar.MONTH constant −calendar.add(Calendar.MONTH, 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()); // Add 3 months to the Calendar calendar.add(Calendar.MONTH, 3); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
1K+ Views
Firstly, you need to import the following package for Calendar class in Javaimport java.util.Calendar;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 year using the calendar.add() method and Calendar.YEAR constant. Set a negative value since we are decrementing herecalendar.add(Calendar.YEAR, -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 Years ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
919 Views
Firstly, you need to import the following package for Calendar class in Javaimport java.util.Calendar;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 add year using the calendar.add() method and Calendar.YEAR constantcalendar.add(Calendar.YEAR, 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()); // Add 20 Years calendar.add(Calendar.YEAR, 20); System.out.println("Updated Date = " + ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
1K+ 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
445 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
776 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
168 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
396 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