
In this article, we will take a look at how to get the day of the week using the Java Date and Time API (Java 8 and above),
1. Get day of the week for LocalDateTime Object
With LocalDateTime you can make use of the getDayOfWeek() method to get the DayOfWeek,
import java.time.DayOfWeek;
import java.time.LocalDateTime;
public class Java8DayOfWeekExample1 {
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
DayOfWeek dayOfWeek = currentDateTime.getDayOfWeek();
System.out.println(dayOfWeek);
}
}
SATURDAY
2. Get day of the week for LocalDate Object
import java.time.DayOfWeek;
import java.time.LocalDate;
public class Java8DayOfWeekExample2 {
public static void main(String[] args) {
LocalDate currentDateTime = LocalDate.now();
DayOfWeek dayOfWeek = currentDateTime.getDayOfWeek();
System.out.println(dayOfWeek);
}
}
MONDAY
3. Get day of the week for ZonedDateTime Object
import java.time.DayOfWeek;
import java.time.ZonedDateTime;
public class Java8DayOfWeekExample3 {
public static void main(String[] args) {
ZonedDateTime zonedDateTimeNow = ZonedDateTime.now();
DayOfWeek dayOfWeek = zonedDateTimeNow.getDayOfWeek();
System.out.println(dayOfWeek);
}
}
TUESDAY
4. Get day of the week for Instant Object
As Instant is associated with seconds or mill seconds, we need to convert it to a LocalDate/LocalDateTime/ZonalDateTime in order to get the week of the day!
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class Java8DayOfWeekExample4 {
public static void main(String[] args) {
Instant instantNow = Instant.now();
DayOfWeek dayOfWeek = LocalDateTime.ofInstant(instantNow, ZoneId.of("UTC")).getDayOfWeek();
System.out.println(dayOfWeek);
}
SUNDAY
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- How to Find Downloaded Files on Windows 11 - Windows-11
- Bash How to Save Output of a Command to a Variable - Bash
- How to wrap column text in SharePoint Online Modern List Grid View using JSON formatting - SharePoint
- Display List of Databases in mysql Command Line - MySQL
- How to change user image icon macOS Big Sur - MacOS
- How to destroy PHP session() - PHP
- How to create Gradle Multi-Project in IntelliJ - Gradle
- Microsoft Teams Error code - 6 issue - Teams