Close

Java Date Time - DayOfWeek.get() Examples

Java Date Time Java Java API 


Class:

java.time.DayOfWeek

java.lang.Objectjava.lang.Objectjava.lang.Enumjava.lang.Enumjava.lang.ComparableComparablejava.io.SerializableSerializablejava.time.DayOfWeekjava.time.DayOfWeekjava.time.temporal.TemporalAccessorTemporalAccessorjava.time.temporal.TemporalAdjusterTemporalAdjusterLogicBig

Method:

public int get(TemporalField field)

DayOfWeek.get(TemporalField) returns the value of the specified field from 'this' DayOfWeek instance. If the provided TemporalField is not supported, UnsupportedTemporalTypeException is thrown.

Only ChronoField.DAY_OF_WEEK is supported by this method, all other fields of ChronoField will throw UnsupportedTemporalTypeException.

This method is defined in the interface TemporalAccessor which is implemented by this class.


Examples


package com.logicbig.example.dayofweek;

import java.time.DayOfWeek;
import java.time.temporal.ChronoField;

public class GetFieldExample {

public static void main (String[] args) {
DayOfWeek dayOfWeek = DayOfWeek.THURSDAY;
int i = dayOfWeek.get(ChronoField.DAY_OF_WEEK);
System.out.println(i);
}
}

Output

4




package com.logicbig.example.dayofweek;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoField;

public class GetFieldExample2 {

public static void main (String[] args) {
LocalDate localDate = LocalDate.parse("2016-10-05");
int i = DayOfWeek.from(localDate)
.get(ChronoField.DAY_OF_WEEK);
System.out.println(i);
}
}

Output

3




package com.logicbig.example.dayofweek;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoField;

public class GetFieldExample3 {

public static void main (String[] args) {
LocalDate localDate = LocalDate.parse("2016-10-05");
int i = DayOfWeek.from(localDate)
.get(ChronoField.DAY_OF_MONTH);
System.out.println(i);
}
}

Output

Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfMonth
at java.time.temporal.TemporalAccessor.range(TemporalAccessor.java:174)
at java.time.DayOfWeek.range(DayOfWeek.java:285)
at java.time.temporal.TemporalAccessor.get(TemporalAccessor.java:218)
at java.time.DayOfWeek.get(DayOfWeek.java:318)
at com.logicbig.example.dayofweek.GetFieldExample3.main(GetFieldExample3.java:16)
... 6 more




See Also