Java 1.8 introduced a new API called as Date-Time that is well designed and overcomes the problems related to java.util.date classes.
DayOfWeek is an Enum that was can be to while dealing with days of the week. It respends days of the week from Monday to Sunday.
- MONDAY: Singleton instance with numeric value as 1
- TUESDAY: Singleton instance with numeric value as 2
- WEDNESDAY: Singleton instance with numeric value as 3
- THURSDAY: Singleton instance with numeric value as 4
- FRIDAY: Singleton instance with numeric value as 5
- SATURDAY: Singleton instance with numeric value as 6
- SUNDAY: Singleton instance with numeric value as 7
Example 1: Get day of the week
To get a day of the week in alphanumeric form just use DayofWeek enum followed by the week name.
System.out.println(DayOfWeek.MONDAY);
System.out.println(DayOfWeek.SUNDAY);
Output:
MONDAY
SUNDAY
Example 2: Get day of the week in numerical equivalent.
To get the day of the week in numeric int value make use of the getValue() method.
System.out.println(DayOfWeek.MONDAY.getValue());
System.out.println(DayOfWeek.SUNDAY.getValue());
Output:
1
7
Example 3: Print day of the week using TextStyle
import java.time.DayOfWeek;
import java.time.format.TextStyle;
import java.util.Locale;
public class Example {
public static void main(String... args) {
printWeekOfDayTextStyles(DayOfWeek.MONDAY);
printWeekOfDayTextStyles(DayOfWeek.TUESDAY);
printWeekOfDayTextStyles(DayOfWeek.WEDNESDAY);
printWeekOfDayTextStyles(DayOfWeek.THURSDAY);
printWeekOfDayTextStyles(DayOfWeek.FRIDAY);
printWeekOfDayTextStyles(DayOfWeek.SATURDAY);
printWeekOfDayTextStyles(DayOfWeek.SUNDAY);
}
public static void printWeekOfDayTextStyles(DayOfWeek dayOfWeek) {
String day = "Day: " + dayOfWeek +"\n";
String fullName = "FULL: "+ dayOfWeek.getDisplayName(TextStyle.FULL,Locale.US)+"\n";
String fullStandAloneName = "FULL_STANDALONE: "+ dayOfWeek.getDisplayName(TextStyle.FULL_STANDALONE,Locale.US)+"\n";
String shortName = "SHORT: "+ dayOfWeek.getDisplayName(TextStyle.SHORT,Locale.US)+"\n";
String shortStandAloneName = "SHORT: "+ dayOfWeek.getDisplayName(TextStyle.SHORT_STANDALONE,Locale.US)+"\n";
String narrowName = "NARROW: "+ dayOfWeek.getDisplayName(TextStyle.NARROW,Locale.US)+"\n";
String narrowStandAloneName = "NARROW_STANDALONE: "+ dayOfWeek.getDisplayName(TextStyle.NARROW_STANDALONE,Locale.US)+"\n";
System.out.println(day + fullName + fullStandAloneName + shortName + shortStandAloneName + narrowName + narrowStandAloneName);
}
}
Output:
Day: MONDAY
FULL: Monday
FULL_STANDALONE: Monday
SHORT: Mon
SHORT: Mon
NARROW: M
NARROW_STANDALONE: M
Day: TUESDAY
FULL: Tuesday
FULL_STANDALONE: Tuesday
SHORT: Tue
SHORT: Tue
NARROW: T
NARROW_STANDALONE: T
Day: WEDNESDAY
FULL: Wednesday
FULL_STANDALONE: Wednesday
SHORT: Wed
SHORT: Wed
NARROW: W
NARROW_STANDALONE: W
Day: THURSDAY
FULL: Thursday
FULL_STANDALONE: Thursday
SHORT: Thu
SHORT: Thu
NARROW: T
NARROW_STANDALONE: T
Day: FRIDAY
FULL: Friday
FULL_STANDALONE: Friday
SHORT: Fri
SHORT: Fri
NARROW: F
NARROW_STANDALONE: F
Day: SATURDAY
FULL: Saturday
FULL_STANDALONE: Saturday
SHORT: Sat
SHORT: Sat
NARROW: S
NARROW_STANDALONE: S
Day: SUNDAY
FULL: Sunday
FULL_STANDALONE: Sunday
SHORT: Sun
SHORT: Sun
NARROW: S
NARROW_STANDALONE: S
Example 4: Using Locale
Let's modify the above code to make use of Locale.
import java.time.DayOfWeek;
import java.time.format.TextStyle;
import java.util.Locale;
public class Example {
public static void main(String... args) {
printWeekOfDayTextStyles(DayOfWeek.MONDAY,Locale.FRANCE);
printWeekOfDayTextStyles(DayOfWeek.TUESDAY,Locale.FRANCE);
printWeekOfDayTextStyles(DayOfWeek.WEDNESDAY,Locale.FRANCE);
printWeekOfDayTextStyles(DayOfWeek.THURSDAY,Locale.FRANCE);
printWeekOfDayTextStyles(DayOfWeek.FRIDAY,Locale.FRANCE);
printWeekOfDayTextStyles(DayOfWeek.SATURDAY,Locale.FRANCE);
printWeekOfDayTextStyles(DayOfWeek.SUNDAY,Locale.FRANCE);
}
public static void printWeekOfDayTextStyles(DayOfWeek dayOfWeek, Locale locale) {
String localeStr = "Locale: " + locale +"\n";
String day = "Day: " + dayOfWeek +"\n";
String fullName = "FULL: "+ dayOfWeek.getDisplayName(TextStyle.FULL,locale)+"\n";
String fullStandAloneName = "FULL_STANDALONE: "+ dayOfWeek.getDisplayName(TextStyle.FULL_STANDALONE,locale)+"\n";
String shortName = "SHORT: "+ dayOfWeek.getDisplayName(TextStyle.SHORT,locale)+"\n";
String shortStandAloneName = "SHORT: "+ dayOfWeek.getDisplayName(TextStyle.SHORT_STANDALONE,locale)+"\n";
String narrowName = "NARROW: "+ dayOfWeek.getDisplayName(TextStyle.NARROW,locale)+"\n";
String narrowStandAloneName = "NARROW_STANDALONE: "+ dayOfWeek.getDisplayName(TextStyle.NARROW_STANDALONE,locale)+"\n";
System.out.println(localeStr + day + fullName + fullStandAloneName + shortName + shortStandAloneName + narrowName + narrowStandAloneName);
}
}
Output:
Locale: fr_FR
Day: MONDAY
FULL: lundi
FULL_STANDALONE: lundi
SHORT: lun.
SHORT: lun.
NARROW: L
NARROW_STANDALONE: L
Locale: fr_FR
Day: TUESDAY
FULL: mardi
FULL_STANDALONE: mardi
SHORT: mar.
SHORT: mar.
NARROW: M
NARROW_STANDALONE: M
Locale: fr_FR
Day: WEDNESDAY
FULL: mercredi
FULL_STANDALONE: mercredi
SHORT: mer.
SHORT: mer.
NARROW: M
NARROW_STANDALONE: M
Locale: fr_FR
Day: THURSDAY
FULL: jeudi
FULL_STANDALONE: jeudi
SHORT: jeu.
SHORT: jeu.
NARROW: J
NARROW_STANDALONE: J
Locale: fr_FR
Day: FRIDAY
FULL: vendredi
FULL_STANDALONE: vendredi
SHORT: ven.
SHORT: ven.
NARROW: V
NARROW_STANDALONE: V
Locale: fr_FR
Day: SATURDAY
FULL: samedi
FULL_STANDALONE: samedi
SHORT: sam.
SHORT: sam.
NARROW: S
NARROW_STANDALONE: S
Locale: fr_FR
Day: SUNDAY
FULL: dimanche
FULL_STANDALONE: dimanche
SHORT: dim.
SHORT: dim.
NARROW: D
NARROW_STANDALONE: D
Locale: de_DE
Day: MONDAY
FULL: Montag
FULL_STANDALONE: Montag
SHORT: Mo.
SHORT: Mo
NARROW: M
NARROW_STANDALONE: M
Locale: de_DE
Day: TUESDAY
FULL: Dienstag
FULL_STANDALONE: Dienstag
SHORT: Di.
SHORT: Di
NARROW: D
NARROW_STANDALONE: D
Locale: de_DE
Day: WEDNESDAY
FULL: Mittwoch
FULL_STANDALONE: Mittwoch
SHORT: Mi.
SHORT: Mi
NARROW: M
NARROW_STANDALONE: M
Locale: de_DE
Day: THURSDAY
FULL: Donnerstag
FULL_STANDALONE: Donnerstag
SHORT: Do.
SHORT: Do
NARROW: D
NARROW_STANDALONE: D
Locale: de_DE
Day: FRIDAY
FULL: Freitag
FULL_STANDALONE: Freitag
SHORT: Fr.
SHORT: Fr
NARROW: F
NARROW_STANDALONE: F
Locale: de_DE
Day: SATURDAY
FULL: Samstag
FULL_STANDALONE: Samstag
SHORT: Sa.
SHORT: Sa
NARROW: S
NARROW_STANDALONE: S
Locale: de_DE
Day: SUNDAY
FULL: Sonntag
FULL_STANDALONE: Sonntag
SHORT: So.
SHORT: So
NARROW: S
NARROW_STANDALONE: S
Locale: zh_CN
Day: MONDAY
FULL: ζζδΈ
FULL_STANDALONE: ζζδΈ
SHORT: ε¨δΈ
SHORT: ε¨δΈ
NARROW: δΈ
NARROW_STANDALONE: δΈ
Locale: zh_CN
Day: TUESDAY
FULL: ζζδΊ
FULL_STANDALONE: ζζδΊ
SHORT: ε¨δΊ
SHORT: ε¨δΊ
NARROW: δΊ
NARROW_STANDALONE: δΊ
Locale: zh_CN
Day: WEDNESDAY
FULL: ζζδΈ
FULL_STANDALONE: ζζδΈ
SHORT: ε¨δΈ
SHORT: ε¨δΈ
NARROW: δΈ
NARROW_STANDALONE: δΈ
Locale: zh_CN
Day: THURSDAY
FULL: ζζε
FULL_STANDALONE: ζζε
SHORT: ε¨ε
SHORT: ε¨ε
NARROW: ε
NARROW_STANDALONE: ε
Locale: zh_CN
Day: FRIDAY
FULL: ζζδΊ
FULL_STANDALONE: ζζδΊ
SHORT: ε¨δΊ
SHORT: ε¨δΊ
NARROW: δΊ
NARROW_STANDALONE: δΊ
Locale: zh_CN
Day: SATURDAY
FULL: ζζε
FULL_STANDALONE: ζζε
SHORT: ε¨ε
SHORT: ε¨ε
NARROW: ε
NARROW_STANDALONE: ε
Locale: zh_CN
Day: SUNDAY
FULL: ζζζ₯
FULL_STANDALONE: ζζζ₯
SHORT: ε¨ζ₯
SHORT: ε¨ζ₯
NARROW: ζ₯
NARROW_STANDALONE: ζ₯
Example 5: Add/Subtract days to DayOfWeek
DayOfWeek dayOfWeek = DayOfWeek.SUNDAY;
//Add days to Week
dayOfWeek = dayOfWeek.plus(1);
System.out.println(dayOfWeek);
//Subtract days to Week
dayOfWeek = dayOfWeek.minus(2);
System.out.println(dayOfWeek);
Output
MONDAY
SATURDAY
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- How to Add a horizontal line in Android Layout - Android
- Calculate days between dates using dateutils ddiff command - Linux
- Reading JSON file in Python with Examples - Python
- Error 404 Tomcat homepage http://localhost:8080/ not displayed - Tomcat
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..] - Java
- pip install see the list of all available versions of package - PIP
- Tomcat Manager Default Username and Password - Tomcat
- Open Terminal in Finder Current Location - MacOS