在Java中, DayOfWeek是一个代表一周中7天的枚举。除了文本枚举名称外, 每周的每一天都有一个int值。
Java DayOfWeek枚举声明
让我们看看java.time.DayOfWeek的声明。
public enum DayOfWeek extends Enum<DayOfWeek> implements TemporalAccessor, TemporalAdjuster
Java DayOfWeek的方法
方法 | 描述 |
---|---|
int get(TemporalField field) | 它用于从一周中的这一天中以整数形式获取指定字段的值。 |
boolean isSupported(TemporalField field) | 用于检查是否支持指定的字段。 |
DayOfWeek minus(long days) | 它用于返回星期几, 该星期几是该天之前的指定天数。 |
DayOfWeek plus(long days) | 它用于返回星期几, 该星期几是该天之后的指定天数。 |
static DayOfWeek of(int dayOfWeek) | 它用于从int值获取DayOfWeek的实例。 |
static DayOfWeek[] values() | 它用于按声明顺序返回包含此枚举类型的常量的数组。 |
Java DayOfWeek示例:get()
import java.time.*;
import java.time.temporal.ChronoField;
public class DayOfWeekExample1 {
public static void main(String[] args) {
LocalDate localDate = LocalDate.of(2017, Month.JANUARY, 25);
DayOfWeek dayOfWeek = DayOfWeek.from(localDate);
System.out.println(dayOfWeek.get(ChronoField.DAY_OF_WEEK));
}
}
立即测试
输出:
3
Java DayOfWeek示例:of()
import java.time.DayOfWeek;
public class DayOfWeekExample2 {
public static void main(String[] args) {
DayOfWeek day = DayOfWeek.of(5);
System.out.println(day.name());
System.out.println(day.ordinal());
System.out.println(day.getValue());
}
}
立即测试
输出:
FRIDAY
4
5
Java DayOfWeek示例:加号()
import java.time.*;
public class DayOfWeekExample3 {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2017, Month.JANUARY, 31);
DayOfWeek day = DayOfWeek.from(date);
System.out.println(day.getValue());
day = day.plus(3);
System.out.println(day.getValue());
}
}
立即测试
输出:
2
5
Java DayOfWeek示例:minus()
import java.time.*;
public class DayOfWeekExample4 {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2017, Month.JANUARY, 31);
DayOfWeek day = DayOfWeek.from(date);
System.out.println(day.getValue());
day = day.minus(3);
System.out.println(day.getValue());
}
}
立即测试
输出:
2
6
评论前必须登录!
注册