HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Java 8 – DayOfWeek

Java 8 – DayOfWeek

Java example to determine which day of the week is a given date. The weekdays are from Sunday, Monday till Saturday.

1. DayOfWeek Enum

DayOfWeek is an enum representing the seven days of the week – Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.

As it is an enum, it has ordinal values associated with each day. It is from 1 (Monday) to 7 (Sunday). Some locales also assign different numeric values to the days, declaring Sunday to have the value 1, however, this class provides no support for this.

To get the numeric representation, use of getValue() is recommended. This is an immutable and thread-safe enum.

2. DayOfWeek for given LocalDate

LocalDate class has method getDayOfWeek() which return the enum value representing that day of the week.

LocalDate today = LocalDate.now();
		
System.out.println( today.getDayOfWeek() );				// SUNDAY
System.out.println( today.getDayOfWeek().getValue() );	// 7

Similar to LocalDate, other temporal classes also provide this method.

  • LocalDate getDayOfWeek()
  • LocalDateTime getDayOfWeek()
  • ZonedDateTime getDayOfWeek()

3. Locale specific value

Use getDisplayName(TextStyle, Locale) to get the value of day of the week in locale specific manner.

public static void main(String[] args) 
{	
	String textValue = getDayString(today, Locale.getDefault());
	
	System.out.println(textValue);			// Sunday
	
	textValue = getDayString(today, Locale.GERMAN);
	
	System.out.println(textValue);			// Sonntag
}

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

Drop me your questions related to getting the day of the week in Java 8.

Happy Learning !!

Sourcecode Download

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Date Time Tutorial

  • Java – Date Time APIs
  • Java – Date Parsing
  • Java – Date Formatting
  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – Period
  • Java 8 – DateTimeFormatter
  • Java 8 – TemporalAdjusters
  • Java 8 – TemporalQuery
  • Java 8 – DayOfWeek
  • Java – Date
  • Java – Locale

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)