在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() | 它用于按声明顺序返回包含此枚举类型的常量的数组。 |
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());
}
}
立即测试
【Java DayOfWeek枚举】输出:
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
推荐阅读
- Java Duration类
- Java日期格式
- Java日期和时间
- Java Clock类
- 运维|一个程序员的水平能差到什么程度......
- 架构|工程师如何成功转技术管理
- java|来自2.5年前端小白的总结
- Android和iOS的13款最佳漫画阅读器应用/软件推荐合集
- 突破专业机柜电源 让机房安全起来