代码之家  ›  专栏  ›  技术社区  ›  Tom

如何计算属于基于ISO的周数的天数

  •  0
  • Tom  · 技术社区  · 7 年前

    我有一个基于iso的周数,它是使用以下java8localdateapi计算的

    int weekNumOfYear = LocalDate#get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)
    

    给定年份和周数(例如,201927),如何计算本周(201917)的开始日期和结束日期?

    我在用 Calendar 类来解决此问题,但不确定它是否正确(特别是,is是否遵循了iso格式)

    更新以下代码对于201953不正确,有 2019年第53周

    @Test
        public void testGetWeekDays() {
            Integer year = 2019;
            Integer week = 27;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
    
            //Use ISO format
            cal.setMinimalDaysInFirstWeek(4);
    
            cal.setFirstDayOfWeek(Calendar.MONDAY);
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.WEEK_OF_YEAR, week);
            cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
            String beginDate = sdf.format(cal.getTime());
            cal.add(Calendar.DAY_OF_WEEK, 6);
            String endDate = sdf.format(cal.getTime());
            System.out.println(beginDate);
            System.out.println(endDate);
        }
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   Ryuzaki L    7 年前

    你也可以使用 with 一周的开始和结束

    System.out.println(localDate.with(DayOfWeek.MONDAY));
    System.out.println(localDate.with(DayOfWeek.SUNDAY));
    

    如果您想从第几周中获得开始日和结束日,请使用 ISO_WEEK_DATE

    LocalDate startDay = LocalDate.parse("2019-W26-1", DateTimeFormatter.ISO_WEEK_DATE);
    
    LocalDate endDay = LocalDate.parse("2019-W26-7", DateTimeFormatter.ISO_WEEK_DATE);
    

    一个数字代表一周中的一天。值从星期一(1)到星期日(7)运行。

        2
  •  2
  •   Basil Bourque    7 年前

    TL;博士

    给定年份和周数(例如,201927),我可以计算本周(201917)的开始日和结束日吗?

    YearWeek              // Represent a particular week of a week-based year.
    .of(                  // Factory method, rather than calling `new`. 
        2019 ,            // The number of the desired week-based year ( *not* calendar year! ).
        26                // Week of the year, 1 through 52 or 53. 
    )
    .atDay(               // Generate a `LocalDate` object for a day within this week.
        DayOfWeek.MONDAY  // Specify the day-of-week.
    )                     // Generate a `LocalDate` for the first day of this week.
    .plusDays( 6 )        // Generate a `LocalDate` representing the last day of this week. 
    

    额外310 -” YearWeek

    使用标准周更容易 org.threeten.extra.YearWeek 中提供的类 ThreeTen-Extra 项目。该项目为现代 java.time公司 Java 8及更高版本中内置的类。

    如果你有一个 ISO 8601 表示 standard week ,分析。

    String input = "2019-W27";
    YearWeek yw = YearWeek.parse( input);
    

    yw.tostring():2019-w27年

    标准的一周从星期一开始。这个 一周 对象可以生成 LocalDate 一周中的任何一天。使用指定日期 DayOfWeek 枚举。

    LocalDate ld = yw.atDay( DayOfWeek.MONDAY);
    

    ld.tostring():2019-07-01年

    为了得到这周剩下的时间,你可以一遍又一遍地加上一天。

    LocalDate nextDay = ld.plusDays( 1 ) ;
    

    ld.tostring():2019-07-02年

    要到周末,加六个。

    LocalDate nextDay = ld.plusDays( 6 ) ;
    

    ld.tostring():2019-07-07年

    或者喜欢使用java流。通过调用生成流 LocalDate::datesUntil 是的。

    Stream < LocalDate > stream = ld.datesUntil ( ld.plusWeeks ( 1 ) );
    List < LocalDate > dates = stream.collect ( Collectors.toList () );
    

    日期.tostring():[2019-07-01、2019-07-02、2019-07-03、2019-07-04、2019-07-05、2019-07-06、2019-07-07]

    顺便说一下,在标准周内表示一周中的一天的标准方法是附加一个连字符和数字1-7。

    2019-W26-1=周一

    2019-W26-2=星期二

    2019-W26-7=周日