Skip Top Navigation Bar

LocalDate Class

In my days as a teacher of AP Computer Science A (AP CSA), I predominately worked inside the Java subset put forth by the College Board. This meant that when I was representing dates with my students, I typically did one of three ways:

All of these ways are the wrong way to represent the immutable data of a date. Representing them in this way means that manipulating and comparing dates becomes extremely difficult. For example, say you had an appointment for March 2nd and you wanted to set a remind 3 days prior to the appointment. To do this you would have to perform a host of checks to ensure you had a valid date for the reminder:

An alternative that allows students to do a whole host of comparisons and time related operations would be to use the LocalDate class. Using the LocalDate class in AP CSA, enhances the course by providing students with another class to practice creating objects and calling methods. One of the skills that students need to perform on the AP CSA exam is to create objects and call methods of classes they have not yet been exposed. Many of the free response questions include a partially implemented class to use in writing solutions. Use of LocalDate, while not specifically called out in the AP Java subset, allows students to practice this skill in relevant contexts.

To use the LocalDate class, you will need to include the import statement:

import java.time.LocalDate;

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();

So, how do you create a LocalDate object? There are a couple ways to do this depending on what you are trying to accomplish. I’ll present a few common possibilities below.

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 method, called now that can be used to create a LocalDate object with today’s date. For example:

LocalDate dateJoined = LocalDate.now();

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 parse to create a LocalDate object for their birthday. For example:

String bday = “2006-04-19”; LocalDate birthday = LocalDate.parse(bday);

Another way you could set the birthday would be to use the of method with the birthday entered as int values. For example:

int year = 2006; 
int month = 4; 
int day = 19; 
LocalDate birthday2 = LocalDate.of (year, month, day);

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:

LocalDate minusDays (long daysToSubtract) 
LocalDate plusDays (long daysToAdd) 

For example:

LocalDate rewardStart = birthday.minusDays (15); 
LocalDate rewardEnd = birthday.plusDays (15);

To check to see if today is in the range from rewardStart to rewardEnd, we can use the following two methods:

boolean isAfter (ChronoLocalDate other) 
boolean isBefore (ChronoLocalDate other) 

For example:

if (today.isAfter(rewardStart) && today.isBefore(rewardEnd)) {      
   System.out.println (“Reward is valid”); 
} else {      
   System.out.println (“Reward is invalid”);
}

There are many more useful methods for LocalDate. To find more, please visit the API for LocalDate here.