Javaexercise.com

How to get day of week from a date in Java 8?

Java getDayOfWeek() method is used to get day of week, such as 'Monday'. This method does not take any argument and returns day-of-week field of DayOfWeek enum. DayOfWeek enum includes textual names of the values, such as Monday, Tuesday etc.

This method is included in four major classes: LocalDate, LocalDateTime, OffsetDateTime and ZonedDateTime class of Java 8 DateTime API which are stored into java.time package.

In this topic, we will learn how to fetch day of week of a date with examples. The syntax of the method is given below.

Syntax

public DayOfWeek getDayOfWeek()

Parameters

This method does not take any parameter.

Return Value

This method returns a textual value of DayOfWeek enum, such as 'Monday'.

Exception

It does not throw any type of exception.

 

We can use getDayOfWeek() method to get day of week, if we have date object of any of these class:


Now, let's see example of each class to understand how to use this method in Java application.

Example: Get day of week using LocalDate date

If you are working with Java LocalDate class and want to get day of week then simply call the getDayOfWeek() method. It will return a full name of day of week.

import java.time.DayOfWeek;
import java.time.LocalDate;

/* 
 * Example to get day of week from a date
 * using the LocalDate class
 */
public class JExercise {
	public static void main(String[] args) {
		
		// Take a date
		LocalDate date = LocalDate.parse("2012-05-02");
		// Print Date
		System.out.println("Date : "+date);
		// Getting Day of week
		DayOfWeek day = date.getDayOfWeek();
		// Display day
		System.out.println("Week day : "+day);
	}
}

Output:

Date : 2012-05-02
Week day : WEDNESDAY
 

Example: Get day of week using LocalDateTime date

If we have a date object of LocalDateTime class and want to get day of week then simply call the getDayOfWeek() method. In this example, we are getting name of day using this method.

import java.time.DayOfWeek;
import java.time.LocalDateTime;

/* 
 * Example to get day of week from a date
 * using the LocalDateTime class
 */
public class JExercise {
	public static void main(String[] args) {
		
		// Take a date
		LocalDateTime date = LocalDateTime.parse("2012-05-02T02:30:10");
		// Print Date
		System.out.println("Date and Time : "+date);
		// Getting Day of week
		DayOfWeek day = date.getDayOfWeek();
		// Display day
		System.out.println("Week day : "+day);
	}
}

Output:

Date and Time : 2012-05-02T02:30:10
Week day : WEDNESDAY
 

Example: Get day of week using OffsetDateTime date

OffSetDateTime class contains the getDayOfWeek() method to get name of day. In this example, we are creating a date using OffsetDateTime class and calling the getDayOfWeek() method to get name of day of week.

import java.time.DayOfWeek;
import java.time.OffsetDateTime;

/* 
 * Example to get day of week from a date
 * using the OffsetDateTime class
 */
public class JExercise {
	public static void main(String[] args) {
		
		// Take a date
		OffsetDateTime date = OffsetDateTime.parse("2012-05-02T02:30:10+01:00");
		// Print Date
		System.out.println("Date and Time : "+date);
		// Getting Day of week
		DayOfWeek day = date.getDayOfWeek();
		// Display day
		System.out.println("Week day : "+day);
	}
}

Output:

Date and Time : 2012-05-02T02:30:10+01:00
Week day : WEDNESDAY

Example: Get day of week using ZonedDateTime date

The ZonedDateTime class is used to control zoned based date and time. If we have a date object of ZonedDateTime class and want to get day of week, call the getDayOfWeek() method. It will return a full name of day as a result.

import java.time.DayOfWeek;
import java.time.ZonedDateTime;

/* 
 * Example to get day of week from a date
 * using the ZonedDateTime class
 */
public class JExercise {
	public static void main(String[] args) {
		
		// Create a ZonedDateTime date
		ZonedDateTime date = ZonedDateTime.parse("2012-05-02T02:30:10.492+05:30[Asia/Calcutta]");
		// Print Date
		System.out.println("Date and Time : "+date);
		// Getting Day of week
		DayOfWeek day = date.getDayOfWeek();
		// Display day
		System.out.println("Week day : "+day);
	}
}

Output:

Date and Time : 2012-05-02T02:30:10.492+05:30[Asia/Calcutta]
Week day : WEDNESDAY
 

Conclusion

Well, in this topic, we learnt To get day of week such as Monday, Tuesday etc by using getDayOfWeek() method. We used several examples to explained the method in better manner. We also learnt that, to get day of week date object should be any of the classes: LocalDate, LocalDateTime, OffsetDateTime and ZonedDateTime.

If we missed something, you can suggest us at - info.javaexercise@gmail.com