Java : DayOfWeek con ejemplos
DayOfWeek (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de DayOfWeek.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
final var date = LocalDate.of(2100, 12, 1);
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println(date.format(formatter)); // Wednesday, December 1, 2100
final var ret = date.getDayOfWeek();
System.out.println(ret); // WEDNESDAY
Enum Constants
FRIDAY
System.out.println(DayOfWeek.FRIDAY); // FRIDAY
MONDAY
System.out.println(DayOfWeek.MONDAY); // MONDAY
SATURDAY
System.out.println(DayOfWeek.SATURDAY); // SATURDAY
SUNDAY
System.out.println(DayOfWeek.SUNDAY); // SUNDAY
THURSDAY
System.out.println(DayOfWeek.THURSDAY); // THURSDAY
TUESDAY
System.out.println(DayOfWeek.TUESDAY); // TUESDAY
WEDNESDAY
System.out.println(DayOfWeek.WEDNESDAY); // WEDNESDAY
Methods
Temporal adjustInto (Temporal temporal)
final var temporal = LocalDate.of(2100, 12, 17);
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println(temporal.format(formatter)); // Friday, December 17, 2100
final var ret1 = DayOfWeek.THURSDAY.adjustInto(temporal);
System.out.println(ret1); // 2100-12-16
final var ret2 = DayOfWeek.FRIDAY.adjustInto(temporal);
System.out.println(ret2); // 2100-12-17
final var ret3 = DayOfWeek.SATURDAY.adjustInto(temporal);
System.out.println(ret3); // 2100-12-18
static DayOfWeek from (TemporalAccessor temporal)
final var temporal = LocalDate.of(2100, 12, 17);
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println(temporal.format(formatter)); // Friday, December 17, 2100
System.out.println(DayOfWeek.from(temporal)); // FRIDAY
final var temporal = LocalDate.of(2100, 12, 18);
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println(temporal.format(formatter)); // Saturday, December 18, 2100
System.out.println(DayOfWeek.from(temporal)); // SATURDAY
int get (TemporalField field)
System.out.println(DayOfWeek.FRIDAY.isSupported(ChronoField.DAY_OF_WEEK)); // true
System.out.println(DayOfWeek.FRIDAY.get(ChronoField.DAY_OF_WEEK)); // 5
System.out.println(DayOfWeek.SUNDAY.isSupported(ChronoField.DAY_OF_WEEK)); // true
System.out.println(DayOfWeek.SUNDAY.get(ChronoField.DAY_OF_WEEK)); // 7
System.out.println(DayOfWeek.FRIDAY.isSupported(ChronoField.YEAR)); // false
try {
final var ret = DayOfWeek.FRIDAY.get(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year
String getDisplayName (TextStyle style, Locale locale)
final var locale = Locale.getDefault();
System.out.println(locale); // en_US
System.out.println(DayOfWeek.FRIDAY.getDisplayName(TextStyle.FULL, locale)); // Friday
System.out.println(DayOfWeek.FRIDAY.getDisplayName(TextStyle.SHORT, locale)); // Fri
System.out.println(DayOfWeek.FRIDAY.getDisplayName(TextStyle.NARROW, locale)); // F
long getLong (TemporalField field)
System.out.println(DayOfWeek.FRIDAY.isSupported(ChronoField.DAY_OF_WEEK)); // true
System.out.println(DayOfWeek.FRIDAY.getLong(ChronoField.DAY_OF_WEEK)); // 5
System.out.println(DayOfWeek.SUNDAY.isSupported(ChronoField.DAY_OF_WEEK)); // true
System.out.println(DayOfWeek.SUNDAY.getLong(ChronoField.DAY_OF_WEEK)); // 7
int getValue ()
System.out.println(DayOfWeek.MONDAY.getValue()); // 1
System.out.println(DayOfWeek.TUESDAY.getValue()); // 2
System.out.println(DayOfWeek.WEDNESDAY.getValue()); // 3
System.out.println(DayOfWeek.THURSDAY.getValue()); // 4
System.out.println(DayOfWeek.FRIDAY.getValue()); // 5
System.out.println(DayOfWeek.SATURDAY.getValue()); // 6
System.out.println(DayOfWeek.SUNDAY.getValue()); // 7
boolean isSupported (TemporalField field)
System.out.println(DayOfWeek.FRIDAY.isSupported(ChronoField.DAY_OF_WEEK)); // true
System.out.println(DayOfWeek.FRIDAY.get(ChronoField.DAY_OF_WEEK)); // 5
System.out.println(DayOfWeek.SUNDAY.isSupported(ChronoField.DAY_OF_WEEK)); // true
System.out.println(DayOfWeek.SUNDAY.get(ChronoField.DAY_OF_WEEK)); // 7
System.out.println(DayOfWeek.FRIDAY.isSupported(ChronoField.YEAR)); // false
try {
final var ret = DayOfWeek.FRIDAY.get(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year
DayOfWeek minus (long days)
final var week = DayOfWeek.FRIDAY;
System.out.println(week); // FRIDAY
final var ret1 = week.minus(1);
System.out.println(ret1); // THURSDAY
final var ret2 = week.minus(2);
System.out.println(ret2); // WEDNESDAY
final var ret3 = week.minus(6);
System.out.println(ret3); // SATURDAY
final var ret4 = week.minus(7);
System.out.println(ret4); // FRIDAY
static DayOfWeek of (int dayOfWeek)
System.out.println(DayOfWeek.of(1)); // MONDAY
System.out.println(DayOfWeek.of(2)); // TUESDAY
System.out.println(DayOfWeek.of(3)); // WEDNESDAY
System.out.println(DayOfWeek.of(4)); // THURSDAY
System.out.println(DayOfWeek.of(5)); // FRIDAY
System.out.println(DayOfWeek.of(6)); // SATURDAY
System.out.println(DayOfWeek.of(7)); // SUNDAY
DayOfWeek plus (long days)
final var week = DayOfWeek.FRIDAY;
System.out.println(week); // FRIDAY
final var ret1 = week.plus(1);
System.out.println(ret1); // SATURDAY
final var ret2 = week.plus(2);
System.out.println(ret2); // SUNDAY
final var ret3 = week.plus(6);
System.out.println(ret3); // THURSDAY
final var ret4 = week.plus(7);
System.out.println(ret4); // FRIDAY
<R> R query (TemporalQuery<R> query)
final var ret = DayOfWeek.FRIDAY.query(TemporalQueries.precision());
System.out.println(ret); // Days
ValueRange range (TemporalField field)
final var ret = DayOfWeek.FRIDAY.range(ChronoField.DAY_OF_WEEK);
System.out.println(ret); // 1 - 7
static DayOfWeek valueOf (String name)
System.out.println(DayOfWeek.valueOf("FRIDAY")); // FRIDAY
System.out.println(DayOfWeek.valueOf("SATURDAY")); // SATURDAY
System.out.println(DayOfWeek.valueOf("SUNDAY")); // SUNDAY
static DayOfWeek[] values ()
for (final var week : DayOfWeek.values()) {
System.out.println(week);
}
// Result
// ↓
//MONDAY
//TUESDAY
//WEDNESDAY
//THURSDAY
//FRIDAY
//SATURDAY
//SUNDAY
Methods declared in Enum
clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
Consulte el siguiente enlace.
Related posts
- Ejemplos de API
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset