Skip to content
Advertisement

How can I output an arrraylist of dates?

I’m pretty new to java. My objective is to let a user input a date in “dd/MM/yyyy” format and for me to be able to add these dates to a calendar array list and then display the array with all the dates in it.

JavaScript

I tried searching for answers here and elsewhere but I could not find what I was looking for and the threads I did look into just confused me further. Here is the output I get when I run this code:

Output

If I remove the date format stuff I get this: enter image description here

What I’m looking for is something like: [20-10-1999,12-03-1998,…]

I’m not really understanding how to properly use calendar and would appreciate any help.

Advertisement

Answer

tl;dr

JavaScript

[2021-01-23, 2021-02-17]

Details

Never use Date and SimpleDateFormat classes. These were years ago supplanted by the modern java.time classes defined in JSR 310.

JavaScript

If you have the year, month, and day components as separate numbers, use another factory method.

JavaScript

To display the LocalDate objects, let java.time automatically localize.

JavaScript

Or you can write another custom format as seen in first code snippet, using DateTimeFormatter.ofPattern.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement