Using the LocalDate Class
import statements
To use the LocalDate
class, you will need to include the import statement:
import java.time.LocalDate;
Alternative to Constructors
There isn’t a constructor for the LocalDate
class, so you aren’t able to create a LocalDate
object by doing:
LocalDate today = new LocalDate();
Your Turn
Let's try it in the Java Playground.
- You will get an error that states that the constructor cannot be applied.
Creating LocalDate Objects
There are a couple ways to create a LocalDate
object.
Scenario 1
Suppose you are writing a rewards app and want to keep track of when a user has joined the app. This allows you to give them a special reward once they have been a member for a year. There is a class method, called now
that can be used to create a LocalDate
object with today’s date. A class method is called using the class name instead of an object.
Your Turn
Let's try it in the Java Playground.
- Predict the date that would be assigned to
dateJoined
- Run the code to determine if your prediction is correct
- Note that the date is listed as year-month-day
Scenario 2
Perhaps this same rewards app wants to record the user’s birthday to give them a free cookie every year around their birthday. After obtaining the String
representation of their birthday, which is April 19, 2006, entered as 2006-04-19
, we can use the class method parse
to create a LocalDate
object for their birthday.
General Format:
LocalDate variableName = LocalDate.parse(<string in the format year-month-day>);
Your Turn
Let's try it in the Java Playground.
- Predict the value of birthday.
- Run the code to detemine whether your prediction is correct.
- Create a new
LocalDate
object for your birthday using the parse
method.
Scenario 3
Another way you could set the birthday would be to use the class method of
with the birthday entered as int
values.
General Format:
LocalDate variableName = LocalDate.of(yearValue, monthValue, dayValue);
Your Turn
Let's try it in the Java Playground.
- Predict the value of birthday.
- Run the code to determine whether your prediction is correct
- Create a new
LocalDate
object with your birthday using the of
method.
Other Methods in LocalDate
Now that you have a date, there are some helpful methods that you can use to work with the date.
LocalDate minusDays(long daysToSubtract) and LocalDate plusDays(long daysToAdd)
Maybe we want to allow the user to receive their reward in a 30-day range around their birthday, 15 days prior and 15 days after their birthday. To do this, we need to obtain these dates. There are two methods that can be used to accomplish this:
General Format:
LocalDate variableName1 = dateVariable.minusDays(<number of days>);
LocalDate variableName2 = dateVariable.plusDays(<number of days>);
Your Turn
Let's try it in the Java Playground.
- Predict the values of
rewardStart
and rewardEnd
.
- Run the code to determine if your prediction is correct.
- Create a new
LocalDate
object with today's date.
- Create a new
LocalDate
object for a day 30 days in the future from today's date.
- Create a new
LocalDate
object for a day 30 days in the past from today's date.
boolean isAfter(ChronoLocalDate other) and boolean isBefore (ChronoLocalDate other)
We can check to see if one LocalDate
is before or after another LocalDate
with the following methods:
boolean isAfter (ChronoLocalDate other)
returns true
if this LocalDate
is chronologically after other
; false
otherwise.
boolean isBefore (ChronoLocalDate other)
returns true
if this LocalDate
is chronologically before other
; false
otherwise.
General Format:
boolean answer1 = oneLocalDateObj.isAfter(anotherLocalDateObj);
boolean answer2 = oneLocalDateObj.isBefore(anotherLocalDateObj);
Your Turn
Let's try it in the Java Playground.
- Predict the values of the
LocalDate
variables, afterStart
, and beforeStart
.
- Run the code to determine if your predictions are correct.
- Write a statement to determine if
birthday
is after rewardEnd
- Write a statement to determine if
birthday
is before rewardEnd
int getYear() and LocalDate withYear(int year)
We have a birthday every year. In this code segment, we obtained the current year from today by using the getYear()
method and then updated birthday to have the current year in the birthday by using the withYear()
method.
Your Turn
Let's try it in the Java Playground
- Predict the values of
year
and birthday
.
- Run the code and determine if your prediction is correct.
- Add 1 to year.
- Create a
LocalDate
object nextBirthday
and assign it to the birthday of the following year.
There are many more useful methods for LocalDate
. To find more, please visit the LocalDate
API.
Resources
Next Learn Tutorial