Found 33676 Articles for Programming

Get the day of week for a particular date in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:10:49

4K+ Views

To get the day of week for a particular date in Java, use the Calendar.DAY_OF_WEEK constant.Let us set a date first.Calendar one = new GregorianCalendar(2010, Calendar.JULY, 10);Since, we have used the Calendar as well as GregorianCalendar classes, therefore at first, import the following packages.import java.util.Calendar; import java.util.GregorianCalendar;Now, we will find the day of week.int day = one.get(Calendar.DAY_OF_WEEK);The following is an exampleExample Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] argv) throws Exception {       Calendar one = new GregorianCalendar(2010, Calendar.JULY, 10);       int day = one.get(Calendar.DAY_OF_WEEK);       System.out.println(day);   ... Read More

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

Samual Sam
Updated on 29-Jun-2020 05:11:29

226 Views

To compare dates if a date is before another date, use the Calendar.before() method.The Calendar.before() method returns whether this Calendar's time is before the time represented by the specified Object. First, let us set a date which is before the current dateCalendar date1 = Calendar.getInstance(); date1.set(Calendar.YEAR, 2010); date1.set(Calendar.MONTH, 11); date1.set(Calendar.DATE, 20);Here is our current dateCalendar date = Calendar.getInstance();Now, use the after() method to compare both the dates as shown in the following exampleThe following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar date1 = Calendar.getInstance();       ... Read More

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

Shriansh Kumar
Updated on 01-Aug-2024 11:48:16

1K+ Views

In Java, there are a few scenarios where we are required to work 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. In this article, we will discuss how to compare and check if a date is after another or not. Example Scenario 1 Input: dateOne = "2023-01-20"; ... Read More

Java program to subtract 1 year from the calendar

Samual Sam
Updated on 05-Sep-2024 11:21:31

2K+ Views

In this article, we will subtract one year from the calendar in Java. We will be using Calender class from the java.util package. The program captures the current date, subtracts one year from it, and then displays the updated date. Problem Statement Write a Java program to subtract one year from the calendar. Below is a demonstration of the same − Input Current Date = Fri Nov 23 06:39:40 UTC 2018 Output Updated Date = Thu Nov 23 06:39:40 UTC 2017 Steps to subtract 1 year from the calendar Following are the steps to subtract 1 year from the calendar − ... Read More

Java program to add 3 months to the calendar

Samual Sam
Updated on 16-Sep-2024 23:25:26

1K+ Views

In this article, we will learn to add 3 months to the calendar. We will take the current date and add 3 months to it using Calendar class in Java. You’ll see how to handle date manipulation and update the date accordingly. Problem Statement Write a program in Java to add 3 months to the calendar. Below is the demonstration of the same − Input Current Date = Fri Nov 23 06:38:56 UTC 2018 Output Updated Date = Sat Feb 23 06:38:56 UTC 2019 Different approaches Below are the different approaches to add 3 months to the calendar − ... 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

668 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

Advertisements