Sometimes when developing Java software, you might need to instantiate LocalDate for a specific date.
In other words, for some date other than today.
Fortunately, you can do that quite easily.
And in this guide, I'll show you how.
A Little Ditty About of()
When it comes to creating an object that represents a specific date, there are a few ways to skin the cat.
One of the simplest is to use the of() method.
LocalDate localDate = LocalDate.of(2021, 4, 1);
That's going to give you April Fool's Day in the year 2021. Or April 1, 2021.
The of() method (in this case) takes three (3) integer parameters:
- The year
- The month
- The day of the month
In that order.
Just keep in mind: that method will throw a DateTimeException if any of the values are out of range.
So that's an easy way to do it if you're anxious to get something going in a hurry.
But It's Overloaded
The method of() is overloaded. That means there's more than one of() method with different method signatures.
The second of() method takes a Month enum as the second parameter.
LocalDate localDate = LocalDate.of(2021, Month.APRIL, 1);
That should be fairly self-explanatory.
If you're using an IDE like Eclipse or Spring Tool Suite, you can usually just type the word "Month" with a period after it and the IDE will show you all the options. You should see every month in the year.
Playing With parse()
But there's another method you can use to get a specific date: parse().
That method takes in a CharSequence. But that means you can send in a String because String implements CharSequence.
So try this:
LocalDate localDate = LocalDate.parse("2021-04-01");
Once again, that will give you April 1, 2021.
But note how you have to structure the string: YYYY-MM-DD.
Put the date parts in that exact order. And use a dash as the separator.
Unless, of course, you'd like to use a custom date formatter.
But It's Overloaded, Too
Like the of() method, parse() is also overloaded.
You can pass in a second parameter that's a DateTimeFormatter type.
Let's say you wanted to instantiate LocalDate for specific date but without using dashes. You can do that as follows.
LocalDate localDate = LocalDate.parse("20210401", DateTimeFormatter.BASIC_ISO_DATE);
That will do it for you.
You'll find that DateTimeFormatter includes several types of predefined formatters that you can use. BASIC_ISO_DATE is just one of them.
If you want to see all the options, check the API doc I linked to above.
But let's say you want to use a custom formatter. Maybe you want to instantiate the day using MM/DD/YYYY (traditional American) format.
You can do that with the ofPattern() method.
LocalDate localDate = LocalDate.parse("04/01/2021", DateTimeFormatter.ofPattern("MM/dd/yyyy"));
Note that the pattern specified in the ofPattern() method must match the formatter symbols defined in the API doc. Again, just click that link above.
Wrapping It Up
There you have it. A few ways to instantiate a LocalDate object with a specific date on the calendar.
Feel free to take the code that you see here and use it in your own Java applications.
Photo by from