Found 34494 Articles for Programming

Java Program to compare dates if a date is after another date

karthikeya Boyini
Updated on 10-Aug-2023 12:57:20

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

Java Program to subtract 1 year from the calendar

Samual Sam
Updated on 27-Jun-2020 13:53:38

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

Java Program to add 3 months to the calendar

Samual Sam
Updated on 27-Jun-2020 13:49:52

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

Java Program to subtract year from current date

Samual Sam
Updated on 27-Jun-2020 13:32:01

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

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

karthikeya Boyini
Updated on 27-Jun-2020 13:33:42

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

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

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

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

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

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

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

Java Program to add days to current date using Java Calendar

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

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

Decrement a Month using the Calendar Class in Java

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

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

Increment a Month using the Calendar Class in Java

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

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

Advertisements