Java -Day of the week using Java 8 DayOfWeek Enum


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




Recent Posts:




Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.