If in your tests you need to generate date values representing, let’s say, today’s date, or yesterday’s, or one year from today, or the last day of the current month, this post will help you do just that. Using Java’s LocalDate, you will be able to generate the date (meaning year, month, day) your tests require easily.

The beauty of this approach is that you yourself, the tester, do not need to know what the current date is, since this will be generated each time the test runs. So, let’s say you want the test to perform a specific action if today is Friday. You can find out if the day when the test runs is a Friday easily with LocalDate. Also, let’s say you want to type into a field the last day of the current month. But as the test runs for months, that day might be the 30th, 31st, 28th or even 29th. With LocalDate this can also be addressed without much hassle. Read on to see how.

Before doing anything, in the test class where the dates will be generated, the import of the LocalDate class must be specified in the import section, as follows:

import java.time.LocalDate;
Today

Now, in the test class, a LocalDate variable needs to be created. In this example, it will be initialized with the value of today’s date and will be called ‘currentDate’:

 LocalDate currentDate = LocalDate.now();

So, let’s assume that today is the 22nd of September 2019. The value stored in the ‘currentDate’ variable will be: 2019-09-22.

Get day of month

Let’s say you need to know what day of the month it is today, as a number. Meaning, is it the 1st, the 12th, the 22th? Based on the current date, which is already stored to the ‘currentDate’ variable, we can retrieve what day of the month it is by using the ‘getDayOfMonth()’ method. This returns an int.

currentDate.getDayOfMonth()

For example, if today is the 22nd of September 2019, it will return: 22.

Get current day of the week

Now let’s say you want to know what day of the week today is: is it Sunday? Monday? For that, again using the ‘currentDate’ variable, and the method ‘getDayOfWeek()’, we can retrieve the String representing the day of the week in upper case:

currentDate.getDayOfWeek()

If for example today is the 22nd of September 2019, that means it’s a Sunday and the result of the above method call will be: SUNDAY.

Get month of the year

If you’re interested in knowing what month today is in, there are two possibilities. The first one retrieves the current month name in uppercase, as a String, by calling the ‘getMonth()’ method on the ‘currentDate’ variable:

currentDate.getMonth()

If today is the 22nd of September 2019, the returned String will be: SEPTEMBER.

The second possibility is to get the month as a number. That means that if for the same date of the 22nd of September 2019 we want to retrieve the value ‘9’ as an int, we will use the ‘getMonthValue()’ method on the ‘currentDate’ variable:

currentDate.getMonthValue()
Tomorrow (and some days in the future)

In order to generate tomorrow’s date (based on today, stored in the ‘currentDate’ variable), the ‘plusDays()’ method can be used. It takes an int as parameter which specifies how many days we want to add to the current date. If we want to retrieve tomorrow’s date, we can call the ‘plusDays()’ method by passing in 1 as the parameter:

currentDate.plusDays(1))

If today is the 22nd of September 2019, this method call will return: 2019-09-23.

If, let’s say, we want to get the date corresponding to 3 days from now, we can call the same method but this time passing 3 as the parameter:

currentDate.plusDays(3))

Note that this method call will return a LocalDate type of result, so if you are interested in storing this result to a String variable, you will need to call the toString() method on the result, as such:

currentDate.plusDays(3).toString()
Yesterday (and some days ago)

Now in order to generate yesterdays’ date (or a date a few days ago), a similar method to the one above can be used, namely ‘minusDays()’. It takes an int as parameter, which represents how many days ago we want the new date to be on. If we want to get yesterdays’ date, the value of the parameter will be 1:

currentDate.minusDays(1)

This method also returns a ‘LocalDate’ type of result, so if a String is required instead, the ‘toString()’ method needs to be called on the result:

currentDate.minusDays(1).toString()

The result of calling this method with the value of the parameter 1, for the current date being the 22nd of September 2019, will be: 2019-09-21.

Next month (or some months in the future)

There is also a method available for adding months to the current date, namely ‘plusMonths()’, which also takes an int as parameter, that specifies how many months to add to the current date. The resulting date will be of type ‘LocalDate’, so if a String is required the ‘toString()’ method needs to be called here too.

Adding 3 months to the current date can be done as follows:

currentDate.plusMonths(3)

If the current date is the 22nd of September 2019, the result of this method call will be: 2019-12-22.

Last month (or a few months ago)

Getting a date some months back from the current date will be done using the ‘minusMonths()’ method call, which takes as parameter an int value representing how many months we want to subtract from the current date, and returns a LocalDate type of result.

To get the date exactly 3 months ago from today, the following call can be made:

currentDate.minusMonths(3)

If the current date is the 22nd of September 2019, the result of this method call will be: 2019-06-22.

Next year (or some years in the future)

Getting a date which is years from today can be done using the ‘plusYears()’ method. The int parameter passed to this method specifies how many years in the future we want the result to be. Getting next year’s date based on today as a LocalDate result can be done:

currentDate.plusYears(1)

If today is the 22nd of September 2019 this method call will result in: 2020-09-22

Last year (or some years ago)

Generating a date in the past, by subtracting years from the current date, will be done using the ‘minusYears()’ method. The int parameter passed to it specifies how many years back we want the new date to be on.

Getting last year’s date which results in a LocalDate result can be done:

currentDate.minusYears(1)

If today is the 22nd of September 2019 this method call will result in: 2018-09-22.

How many days there are in a month

If your tests need to know how many days there are in a certain month, this can be found out easily, without you having to write tons of code to figure it out. The ‘lengthOfMonth()’ method will give you this information. If we want to retrieve how many days there are in the current month, based on the ‘currentDate’ variable, you just need to do the following:

currentDate.lengthOfMonth()

If the current date is in September, this will return 30.

Change the day of the current date (or the month, or the year)

Let’s say you have the current date, stored in the ‘currentDate’ variable, and you only want to change either the day, or the month, or the year for this date. This can be done using the dedicated methods: ‘withDayOfMonth()’, ‘withYear()’ and ‘withMonth()’. Each takes an int as parameter, which specifies to what day, year or month you want to set the initial value.

For example, if today is the 22nd of September 2019, but you want to change this to the 3rd of September 2019, you would do the following:

currentDate.withDayOfMonth(3)

This basically changes the value of the day from 22 to the value of the parameter you passed to the ‘withDayOfMonth()’ method, which is 3. The result is: 2019-09-03.

Setting the date to the last day of the month

Let’s say we have the current date stored in the ‘currentDate’ variable. But we want to change the date to the last day of the current month.  Here we will use a combination of the ‘withDayOfMonth()’ and ‘lengthOfMonth()’ methods as such:

currentDate.withDayOfMonth(currentDate.lengthOfMonth()))

So, if the current date is the 22nd of September 2019, the result of the above code is: 2019-09-30.

Formatting the dates

As you already saw, all the generated LocalDate values are in the format of ‘year-month-day’.

If we need the date in a different format, like ‘day-month-year’, we can use DateTimeFormatter, as follows. Let’s say we’re generating a date one week ago (using the ‘minusWeeks()’ method):

LocalDate currentDate = LocalDate.now().minusWeeks(1);

The result stored in the ‘currentDate’ variable, if today is the 22nd of September 2019, would be: 2019-09-15.

Now we need to define the format in which we want the date to be generated, by using the DateTimeFormatter class:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");

Having the pattern defined, we just need to apply it to the ‘currentDate’ variable, by passing it as parameter to the ‘format()’ method as below.

String formattedCurrentDate = currentDate.format(formatter);

The value stored in the ‘formattedCurrentDate’ variable will now be: 15-09-2019.

GitHub location of examples

You can see all the code from this post in my GitHub project: https://github.com/iamalittletester/little-project/blob/master/src/test/java/com/imalittletester/usefuldates/UsefulDatesTest.java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.