Java程序获取星期的开始和结束日期
首先,设置一个日期:
LocalDate date = LocalDate.of(2019, 4, 16);
现在,获取星期几的日期:
LocalDate start = date;while (start.getDayOfWeek() != DayOfWeek.MONDAY) {
start = start.minusDays(1);
}
现在,获取本周结束的日期:
LocalDate end = date;while (end.getDayOfWeek() != DayOfWeek.SUNDAY) {
end = end.plusDays(1);
}
示例
import java.time.DayOfWeek;import java.time.LocalDate;
public class Demo {
public static void main(String[] argv) {
LocalDate date = LocalDate.of(2019, 4, 16);
System.out.println("Date = " + date);
LocalDate start = date;
while (start.getDayOfWeek() != DayOfWeek.MONDAY) {
start = start.minusDays(1);
}
System.out.println("Start of the Week = " + start);
LocalDate end = date;
while (end.getDayOfWeek() != DayOfWeek.SUNDAY) {
end = end.plusDays(1);
}
System.out.println("End of the Week = " + end);
}
}
输出结果
Date = 2019-04-16Start of the Week = 2019-04-15
End of the Week = 2019-04-21
以上是 Java程序获取星期的开始和结束日期 的全部内容, 来源链接: utcz.com/z/345359.html