How to Add Date in Arraylist Java?An ArrayList in Java is a dynamically resizable array that may hold objects of any type. Adding dates to an ArrayList is a regular activity when working with time-based data. In this section, we will discuss the various methods for adding dates to an ArrayList in Java. Method 1: Using the add() functionThe add() function given by the ArrayList class is the easiest way to add a date to an ArrayList. Here's an illustration: ArrayDate.java Output: [Mon May 01 15:52:11 GMT 2023, Sun Apr 30 15:52:11 GMT 2023] Explanation In this example, we build an ArrayList of Date objects and then use the add() function to add two dates to it. The first date is the current date and time, which is retrieved by executing the Date class's no-argument constructor. The second date is one day before the current date and time, which may be calculated by subtracting 24 hours from the current time. Method 2: Using the addAll() functionThe addAll() function adds numerous entries to an ArrayList at once. This function may be used to add numerous dates to an ArrayList. Here's an illustration: DateArrayList.java Output: [Sat Apr 29 16:05:34 GMT 2023, Fri Apr 28 16:05:34 GMT 2023] Explanation In this example, we'll start by making a List of Date objects with two dates, two and three days before the current date and time. We then use the addAll() function to add these dates to our ArrayList. Method 3: Using the set() functionThe set() function allows us to replace an element in an ArrayList at a given index. This function may be used to insert a date at a precise location in an ArrayList. Here's an illustration: DateArrayList.java Output: [Sat Apr 29 16:06:48 GMT 2023, Thu Apr 27 16:06:48 GMT 2023] Explanation First, we add two dates to our ArrayList, two and three days before the current date and time. The set() function is then used to change the second date with a new date that is four days before the current date and time. Method 5: Using the add() function with a given indexIn method 5, we utilize the add() function to put a new Date object into the ArrayList at a specified index. In this scenario, we enter the date three days ago at index one, relocating the previous dates to the right. DataArray.java Output: [Sat Apr 29 16:14:51 GMT 2023, Fri Apr 28 16:14:51 GMT 2023, Thu Apr 27 16:14:51 GMT 2023] Method 6: Using the add() method with a specified object typeMethod 6 uses the ArrayList to hold items of various sorts. Using the add() method, we add two Date objects and a String object to the ArrayList. To enable for the storing of objects of multiple kinds, we define the object type as Object in the ArrayList declaration. We print all items, including the String, when we print the ArrayList. DateArrayListAddObjectTypeMethod.java Output: [Sat Apr 29 16:16:55 GMT 2023, Fri Apr 28 16:16:55 GMT 2023, This is a string] ConclusionFinally, Java has numerous ways to add a date to an ArrayList. The most popular option is to add a new Date object at the end of the ArrayList using the add() function. You may also use the addAll() function to add multiple dates to the ArrayList. We may also use the set() function to replace a date at a given index and the add() method with a specific index to insert a new date at a specific location. We can also use the add() function to add dates to an ArrayList of a certain object type. The program's unique requirements determine the approach chosen. Next TopicHow to Create a Generic List in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India