Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Overview

In this short tutorial, we'll see how to extract the day of the week as a number and as text from a Java date.

2. Problem

Business logic often needs the day of the week. Why? For one, working hours and service levels differ between workdays and weekends. Therefore, getting the day as a number is necessary for a lot of systems. But we also may need the day as a text for display.

So, how do we extract the day of the week from dates in Java?

3. Solution With java.util.Date

java.util.Date has been the Java date class since Java 1.0. Code that started with Java version 7 or lower probably uses this class.

3.1. Day of Week as a Number

First, we extract the day as a number using java.util.Calendar:

public static int getDayNumberOld(Date date) {
    Calendar cal = Calendar.getInstance();
    return cal.get(Calendar.DAY_OF_WEEK);

The resulting number ranges from 1 (Sunday) to 7 (Saturday). Calendar defines constants for this: Calendar.SUNDAY – Calendar.SATURDAY.

3.2. Day of Week as Text

Now we extract the day as text. We pass in a Locale to determine the language:

public static String getDayStringOld(Date date, Locale locale) {
    DateFormat formatter = new SimpleDateFormat("EEEE", locale);
    return formatter.format(date);

This returns the full day in your language, such as “Monday” in English or “Montag” in German.

4. Solution With java.time.LocalDate

Java 8 overhauled date and time handling and introduced java.time.LocalDate for dates. Therefore, Java projects that only run on Java versions 8 or higher should use this class!

4.1. Day of Week as a Number

Extracting the day as a number is trivial now:

public static int getDayNumberNew(LocalDate date) {
    DayOfWeek day = date.getDayOfWeek();
    return day.getValue();

The resulting number still ranges from 1 to 7. But this time, Monday is 1 and Sunday is 7! The day of the week has its own enum — DayOfWeek. As expected, the enum values are MONDAY – SUNDAY.

4.2. Day of Week as Text

Now we extract the day as text again. We also pass in a Locale:

public static String getDayStringNew(LocalDate date, Locale locale) {
    DayOfWeek day = date.getDayOfWeek();
    return day.getDisplayName(TextStyle.FULL, locale);

Just as with java.util.Date, this returns the full day in the chosen language.

5. Conclusion

In this article, we extracted the day of the week from Java dates. We saw how to return both a number and a text using java.util.Date and java.time.LocalDate.

As always, the code is available over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Comments are closed on this article!