It's April 21, 2021 as I write this.

But what day of the week is that?

I can get that info pretty easily by using the Java 8 date and time library.

And in this guide, I'll show you how to do it.

What You Normally Get

If you're familiar with LocalDate, then you know you can easily grab the current date with this line of code:

LocalDate localDate = LocalDate.now();

Print out that object and you'll get something like this:

2021-04-21

But that doesn't get you the day of the week.

Fortunately, you can get it with just another line of code.

Getting the Day of the Week

Here's how to do it:

DayOfWeek dayOfWeek = localDate.getDayOfWeek();

And then if you print that out, you'll see something like this:

WEDNESDAY

That's awesome!

But pay attention to a couple of important points.

First, DayOfWeek is an enum. That's why you see the print-out in all uppercase above.

Second, unlike many enums, DayOfWeek offers a variety of useful methods.

Say, for example, that you're interested in getting the numerical day of the week. You can get that with getValue().

So

dayOfWeek.getValue();

would return the number 3. 

That makes sense because DayOfWeek follows the ISO 8601 standard which enumerates each day from 1 (Monday) to 7 (Sunday).

Pretty Printing

But let's get back to the day of the week in text. It's quite likely that an enum called WEDNESDAY isn't exactly what you're looking for.

Fortunately, DayOfWeek offers a convenience method that enables you to get an easy-to-read and proper-cased String representation.

Take a look at getDisplayName().

String dayOfWeekStr = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.US);

That method accepts two parameters: TextStyle and Locale.

You might not be familiar with TextStyle. It's another enum.

TextStyle gives you several options:

  • FULL
  • FULL_STANDALONE
  • NARROW
  • NARROW_STANDALONE
  • SHORT
  • SHORT_STANDALONE

For starters, all those options that end with _STANDALONE are irrelevant in the English language. In other languages, though, there may be a difference when the name is used alone versus when it's used in a full date.

So for now, just hone in on these three options:

  • FULL - the complete name (Wednesday).
  • SHORT - the abbreviated name (Wed)
  • NARROW - the first letter (W)

The second paramater, Locale, is a class that you're probably already familiar with. It represents a specific geographical, political, or cultural region.

Meaning that it specifies the language for the day of the week.

So let's take another look at this:

String dayOfWeekStr = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.US);

If you print that out, you'll get:

Wednesday

Now try this:

String dayOfWeekStr = dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.US);

Print that out and you'll get:

Wed

Or how about this:

String dayOfWeekStr = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.FRANCE);

Print that out and you'll get:

mercredi

And Google Translate assures me that mercredi is indeed the French word for Wednesday.

Wrapping It Up

Well that was fun.

Now you know how to get the textual representation of the day of the week in your Java application.

Over to you. Take what you've learned here and make it yours.

Have fun!

Photo by Anete Lusina from Pexels