📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Java DayOfWeek Enum Class Diagram
Java DayOfWeek Enum Methods/APIs with Examples
DayOfWeek Enum Methods/APIs
- static DayOfWeek from(TemporalAccessor temporal) - Obtains an instance of DayOfWeek from a temporal object.
- int get(TemporalField field) - Gets the value of the specified field from this day-of-week as an int.
- String getDisplayName(TextStyle style, Locale locale) - Gets the textual representation, such as 'Mon' or 'Friday'.
- long getLong(TemporalField field) - Gets the value of the specified field from this day-of-week as a long.
- int getValue() - Gets the day-of-week int value.
- boolean isSupported(TemporalField field) - Checks if the specified field is supported.
- DayOfWeek minus(long days) - Returns the day-of-week that is the specified number of days before this one.
- static DayOfWeek of(int dayOfWeek) - Obtains an instance of DayOfWeek from an int value.
- DayOfWeek plus(long days) - Returns the day-of-week that is the specified number of days after this one.
- R query(TemporalQuery query) - Queries this day-of-week using the specified query.
- ValueRange range(TemporalField field) - Gets the range of valid values for the specified field.
- static DayOfWeek valueOf(String name) - Returns the enum constant of this type with the specified name.
- static DayOfWeek[] values() - Returns an array containing the constants of this enum type, in the order, they are declared.
DayOfWeek Enum Methods/APIs Examples
package net.javaguides.date;
import java.time.DayOfWeek;
import java.time.ZonedDateTime;
import java.time.format.TextStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
import java.util.Locale;
/**
* Class demonstrates the usage of DayOfWeek methods with examples
*
* @author Ramesh Fadatare
*
*/
public class DayOfWeekMethodExamples {
public static void main(String[] args) {
DayOfWeekMethodExamples examples = new DayOfWeekMethodExamples();
examples.valuesMethodExample();
examples.valueOfMethodExample();
examples.rangeMethodExample();
examples.queryMethodExample();
examples.plusMethodExample();
examples.ofMethodExample();
examples.minusMethodExample();
examples.getValueMethodExample();
examples.getLongMethodExample();
examples.getDisplayNameMethodExample();
examples.getMethodExample();
examples.fromMethodExample();
}
public void valuesMethodExample() {
for (DayOfWeek day: DayOfWeek.values()) {
System.out.print(" " + day);
}
}
public void valueOfMethodExample() {
DayOfWeek day = DayOfWeek.valueOf("WEDNESDAY");
System.out.println(day);
}
public void rangeMethodExample() {
DayOfWeek day = DayOfWeek.of(2);
System.out.println("Range : " + day.range(ChronoField.DAY_OF_WEEK));
}
public void queryMethodExample() {
DayOfWeek day = DayOfWeek.of(2);
System.out.printf("DayOfWeek precision is %s%n", day.query(TemporalQueries.precision()));
}
public void plusMethodExample() {
DayOfWeek day = DayOfWeek.of(5);
System.out.println(day);
System.out.println(day.plus(1));
}
public void ofMethodExample() {
DayOfWeek day = DayOfWeek.of(3);
System.out.println(day);
}
public void minusMethodExample() {
DayOfWeek day = DayOfWeek.of(5);
System.out.println(day);
System.out.println(day.minus(1));
}
public void getValueMethodExample() {
DayOfWeek day = DayOfWeek.of(3);
System.out.println(day.getValue());
}
public void getLongMethodExample() {
DayOfWeek day = DayOfWeek.of(3);
System.out.println(day.getLong(ChronoField.DAY_OF_WEEK));
}
public void getDisplayNameMethodExample() {
DayOfWeek day = DayOfWeek.of(3);
System.out.println(day.getDisplayName(TextStyle.SHORT, Locale.ENGLISH));
}
public void getMethodExample() {
DayOfWeek day = DayOfWeek.FRIDAY;
System.out.println(day.get(ChronoField.DAY_OF_WEEK));
}
public void fromMethodExample() {
DayOfWeek day = DayOfWeek.from(ZonedDateTime.now());
System.out.println(day);
}
}
MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAYWEDNESDAY
Range : 1 - 7
DayOfWeek precision is Days
FRIDAY
SATURDAY
WEDNESDAY
FRIDAY
THURSDAY
3
3
Wed
5
SATURDAY
Comments
Post a Comment
Leave Comment