简体   繁体   English

给定一个calendar.week_of_year,我如何获取一周开始和结束的日期?

[英]Given a calendar.week_of_year, How do I get the dates for the start and end of the week?

Given a week_of_year, how can I get the dates for the start and end of the week? 给定一个week_of_year,我如何获得一周开始和结束的日期?

Example: Let's say the date is Jan/1/2017. 示例:假设日期为2017年1月1日。

Calendar calendar = Calendar.getInstance();
// Let's assume that we've set calendar to Jan/1/2017.
Integer week_of_year = calendar.get(Calendar.WEEK_OF_YEAR)

week_of_year would return 1. Presumably, week 1 is anything between Jan/1/2017 to Jan/7/2017. week_of_year将返回1。大概,第1周是2017年1月1日到2017年7月7日之间的任何时间。

How can I reverse lookup week_of_year=1 and get the min/max of Jan/7/2017 to Jan/6/2017? 我如何才能反向查找week_of_year = 1并获得2017年1月7日至2017年6月6日的最小值/最大值? or for any other valid week_of_year value. 或其他任何有效的week_of_year值。

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.WEEK_OF_YEAR, 1);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        System.out.println("Start Date: " + sdf.format(cal.getTime()));
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        System.out.println("End Date: " + sdf.format(cal.getTime()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM