LocalDate获取一年的周数,及指定周数周一到周日对应得日期-java8
2022/3/30 14:19:37
本文主要是介绍LocalDate获取一年的周数,及指定周数周一到周日对应得日期-java8,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
public class TestWeekNumAndDate { /** * @param args */ public static void main(String[] args) { //初始化,第一周至少四天 WeekFields wfs= WeekFields.of(DayOfWeek.MONDAY, 4); //一年最后一天日期的LocalDate,如果该天获得的周数为1或52,那么该年就只有52周,否则就是53周 //获取指定时间所在年的周数 int num= LocalDate.of(2019, 12, 31).get(wfs.weekOfWeekBasedYear()); num = num == 1 ? 52 : num; System.out.println("第" + num + "周,周一日期:" + getDateByYearAndWeekNumAndDayOfWeek(2019, num, DayOfWeek.MONDAY)); System.out.println("第" + num + "周,周二日期:" + getDateByYearAndWeekNumAndDayOfWeek(2019, num, DayOfWeek.TUESDAY)); System.out.println("第" + num + "周,周三日期:" + getDateByYearAndWeekNumAndDayOfWeek(2019, num, DayOfWeek.WEDNESDAY)); System.out.println("第" + num + "周,周四日期:" + getDateByYearAndWeekNumAndDayOfWeek(2019, num, DayOfWeek.THURSDAY)); System.out.println("第" + num + "周,周五日期:" + getDateByYearAndWeekNumAndDayOfWeek(2019, num, DayOfWeek.FRIDAY)); System.out.println("第" + num + "周,周六日期:" + getDateByYearAndWeekNumAndDayOfWeek(2019, num, DayOfWeek.SATURDAY)); System.out.println("第" + num + "周,周日日期:" + getDateByYearAndWeekNumAndDayOfWeek(2019, num, DayOfWeek.SUNDAY)); //该格式周日为一周第一天,周六为一周最后一天 System.out.println(LocalDate.parse("2019-52-1", DateTimeFormatter.ofPattern("YYYY-ww-e", Locale.CHINA))); } private static LocalDate getDateByYearAndWeekNumAndDayOfWeek(Integer year, Integer num, DayOfWeek dayOfWeek) { //周数小于10在前面补个0 String numStr = num < 10 ? "0" + String.valueOf(num) : String.valueOf(num); //2019-W01-01获取第一周的周一日期,2019-W02-07获取第二周的周日日期 String weekDate = String.format("%s-W%s-%s", year, numStr, dayOfWeek.getValue()); return LocalDate.parse(weekDate, DateTimeFormatter.ISO_WEEK_DATE); } }
这篇关于LocalDate获取一年的周数,及指定周数周一到周日对应得日期-java8的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26消息中间件源码剖析教程
- 2024-11-26JAVA语音识别项目资料的收集与应用
- 2024-11-26Java语音识别项目资料:入门级教程与实战指南
- 2024-11-26SpringAI:Java 开发的智能新利器
- 2024-11-26Java云原生资料:新手入门教程与实战指南
- 2024-11-26JAVA云原生资料入门教程
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程