Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Methods to Add Date in ArrayList Java
2.1.
Using the add() function
2.2.
Java
2.3.
Using Set() Function 
2.4.
Java
2.5.
Using addAll() function
2.6.
Java
2.7.
Using the add() method with a specified object type
2.8.
Java
2.9.
Using the add() method with a given index
2.10.
Java
3.
Frequently Asked Questions
3.1.
How to add a date in Java?
3.2.
How to add a date by one day in Java?
3.3.
How to add 1 year to date in Java?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

How to Add Date in Arraylist Java

Author yuvatimankar
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

The List interface in Java defines an ordered collection of elements. The ArrayList is a resizable array implementation of the List interface that is part of the java.util package. ArrayList grows and shrinks dynamically on the addition and removal of elements, respectively. It also inherits the AbstractList class and executes the RandomAccess, List, and Serializable interfaces. It is comparatively slower than built-in arrays but can be beneficial in lots of array manipulation programs.

how to add date in ArrayList java

In this article, we will see how to add date in ArrayList Java with the help of different methods and examples. So, Let’s get started!

Methods to Add Date in ArrayList Java

There are various methods using which we can add a date in the arraylist. Some of the methods are as follows:

Using the add() function

This is the easiest way to add a date to an ArrayList. In this method, we use the add() function, which is given by the ArrayList class, to add a date to an ArrayList.

Code

  • Java

Java

import java.util.ArrayList;
import java.util.Date;
public class ArrayDateExample {
   public static void main(String[] args) {
       ArrayList<Date> dateList = new ArrayList<Date>();
       /* Adding dates to the ArrayList */
       dateList.add(new Date());
       dateList.add(new Date(System.currentTimeMillis() - 48 * 60 * 60 * 1000));
       /* Printing the ArrayList */
       System.out.println(dateList);
   }
}

Output

output

Explanation

In the above example, we created an ArrayList of Object DateList, and then we used the add() function to add two dates to it. The first date provided is the current date and time, which is accessed by executing the DateList Class’s no-argument constructor. The second date provided is the date two days before the current date and time, which can be calculated by subtracting 48 hours from the current time.

Using Set() Function 

Code

  • Java

Java

import java.util.ArrayList;
import java.util.Date;
public class UpdatedDateArrayList {
   public static void main(String[] args) {
       ArrayList<Date> customDates = new ArrayList<Date>();

       /* Adding custom dates to the ArrayList */
       customDates.add(new Date(System.currentTimeMillis() - 5 * 24 * 60 * 60 * 1000)); // 5 days ago
       customDates.add(new Date(System.currentTimeMillis() - 7 * 24 * 60 * 60 * 1000)); // 7 days ago

       /* Updating a date at a specific index */
       customDates.set(1, new Date(System.currentTimeMillis() - 10 * 24 * 60 * 60 * 1000)); // 10 days ago    
       System.out.println(customDates);
   }
}

Output

output

Explanation

In the above code, we have added two dates to our ArrayList, five and seven days before the current date and time. Then, the set() function is used to change the second date with a new date that is ten days before the current date and time.

Using addAll() function

In this method, we will use the addAll() function to add date in ArrayList. This function adds  numerous entries to an ArrayList at once. This function can be used when we want to add numerous dates to an ArrayList.

Code

  • Java

Java

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class UpdatedDateArrayList {
   public static void main(String[] args) {
       ArrayList<Date> customDates = new ArrayList<Date>();

       /* Creating a list of custom dates */
       List<Date> newCustomDates = new ArrayList<Date>();
       newCustomDates.add(new Date(System.currentTimeMillis() - 4 * 24 * 60 * 60 * 1000)); /* 4 days ago */
       newCustomDates.add(new Date(System.currentTimeMillis() - 6 * 24 * 60 * 60 * 1000)); /* 6 days ago */

       /* Adding the new custom dates to the ArrayList using addAll() */
       customDates.addAll(newCustomDates);

       /* Printing the ArrayList */
       System.out.println(customDates);
   }
}

Output

output

Explanation

In the above example, we started by making a List of Date objects with two dates, four and six days before the current date and time. We then used the addAll() function to add these dates to our ArrayList.

Using the add() method with a specified object type

This method uses the ArrayList to store items of various sorts. We can add a Date Object and a string object to the ArrayList using this method. We define the Object as the object type in ArrayList declaration to enable storing of objects of multiple types. 

Code

  • Java

Java

import java.util.ArrayList;
import java.util.Date;
public class UpdatedArrayListAddObjectType {
   public static void main(String[] args) {
       ArrayList<Object> objectsList = new ArrayList<Object>();

       /*Adding different objects to the ArrayList */
       objectsList.add(new Date(System.currentTimeMillis() - 2 * 24 * 60 * 60 * 1000)); // Date object
       objectsList.add(new Date(System.currentTimeMillis() - 3 * 24 * 60 * 60 * 1000)); // Date object
       objectsList.add("Welcome to Coding Ninjas"); // String object   
       System.out.println(objectsList);
   }
}

Output

output

Explanation

In the above code, we have added Date objects and a String object to the ArrayList. Also, along with the date, we have printed the string. We have created a List of Date Objects with two dates, two and three days before the current date and time.

Using the add() method with a given index

In this method, we have used the add() method to put a new Date Object in the ArrayList at a specified index. In the following example, we have entered the date four days ago at index one, relocating the previous dates to the right.

Code

  • Java

Java

import java.util.ArrayList;
import java.util.Date;

public class UpdatedDataArray {
   public static void main(String[] args) {
       ArrayList<Date> customDates = new ArrayList<Date>();

       /* Adding custom dates to the ArrayList */
       customDates.add(new Date(System.currentTimeMillis() - 3 * 24 * 60 * 60 * 1000)); // 3 days ago
       customDates.add(new Date(System.currentTimeMillis() - 5 * 24 * 60 * 60 * 1000)); // 5 days ago

       /* Adding a new custom date at index 1 */
       customDates.add(1, new Date(System.currentTimeMillis() - 4 * 24 * 60 * 60 * 1000)); // 4 days ago      
       System.out.println(customDates);
   }
}

Output

output
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

How to add a date in Java?

In Java, for adding a date, we use the add() method of the calendar class. The add() method takes two arguments: the calendar field and the amount of time that is required to be added.

How to add a date by one day in Java?

We can use plusDays() method of the java.time.LocalDate class from the package java.time, to increment a date by one day in Java. 

How to add 1 year to date in Java?

To add the number of specified years in this LocalDate and return a copy of LocalDate, the plusYears() method of LocalDate Class in Java is used.

Conclusion

In this article, we have discussed How to Add Date in Arraylist Java. We have seen multiple methods by which we can add Date in ArrayList Java and also seen an example of it for a better understanding.

We hope this blog has helped you enhance your knowledge of how to add date in arraylist java. If you want to learn more, then check out our articles:

Refer to our guided paths on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must have a look at the problemsinterview experiences, and interview bundles for placement preparations.

Nevertheless, consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Previous article
Vector Class
Next article
Difference Between ArrayList and Vector in Java
Live masterclass