故障周数-Java 8

时间:2018-08-06 06:07:47

标签: java-8

我要创建一个“选择”,以便在另一个“选择”中选择年份时,我加载该年的所有星期,从星期一到星期五:

例如,如果我选择2018,则我的“选择”将是这样。

1-01/01/18-07/01/18

2-01/08/18-18/14/18

3-15/01/18-01/21/10

......

52-12/24/18-12/30/18

数据是使用java在后端准备的。然后将其作为字符串数组发送到前端

2 个答案:

答案 0 :(得分:1)

避免遗留的日期时间类

现代解决方案使用 java.time 类。这些取代了有严重缺陷的遗留类,例如 DateCalendar

循环

确定一年的第一天。下一年的第一天,作为我们的限制。

Year year = Year.of( 2018 );
LocalDate firstOfYear = year.atDay( 1 );
LocalDate firstOfFollowingYear = year.plusYears( 1 ).atDay( 1 );

使用 TemporalAdjuster 来穿越时间。 TemporalAdjusters 类(注意复数)恰好为我们的需要提供了一个调整器实现,previousOrSame。如果一年中的第一天还不是星期一,那么使用它可以及时返回到星期一。

TemporalAdjuster adjuster = TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY );
LocalDate firstMondayOfYear = firstOfYear.with( adjuster );

定义一个列表来保存我们的每周报告。

List < String > weeks = new ArrayList <>( 53 );

定义一个格式化程序来为我们的每周报告生成文本。 DateTimeFormatter 类可以自动本地化,因此我们无需硬编码特定格式。

Locale locale = Locale.forLanguageTag( "es-ES" ); // Spain locale.
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( locale );

遍历一年中的几周。我们一次增加一周,直到下一年。

int nthWeekOfYear = 0;
LocalDate localDate = firstMondayOfYear;
while ( localDate.isBefore( firstOfFollowingYear ) ) {
    nthWeekOfYear++;
    String output = nthWeekOfYear + " - " + localDate.format( formatter ) + " - " + localDate.plusDays( 6 ); // Using Fully-Closed weeks, where both beginning and end are inclusive. In contrast to Half-Open.
    weeks.add( output );
    // Set up next loop.
    localDate = localDate.plusWeeks( 1 );
}

根据您在问题中的指示,将 List 转换为数组。

String[] results = weeks.toArray( new String[ weeks.size() ] );

转储到控制台。

System.out.println( "results = " + Arrays.toString( results ) );

我们可以使用流和 lambda 来重写此代码。这需要 Java 9+。

LocalDate#datesUntil 方法生成 StreamLocalDate 对象。

虽然我不一定推荐这段代码,但我发现我们可以将整个例程编写为一行代码很有趣。

int inputYear = 2018;
List < String > weeks =
        Year
                .of( inputYear )
                .atDay( 1 )
                .with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) )
                .datesUntil( Year.of( inputYear ).plusYears( 1 ).atDay( 1 ) , Period.ofWeeks( 1 ) )
                .map( localDate -> localDate.format( DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( Locale.forLanguageTag( "es-ES" ) ) ) + " - " + localDate.plusDays( 6 ).format( DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( Locale.forLanguageTag( "es-ES" ) ) ) )
                .toList();  // Before Java 16, change to .collect( Collectors.toList()); or .collect( Collectors.toUnmodifiableList());

运行时。

<块引用>

周 = [1/1/18 - 7/1/18, 8/1/18 - 14/1/18, 15/1/18 - 21/1/18, 22/1/18 - 28/ 1/18, 29/1/18 - 4/2/18, 5/2/18 - 11/2/18, 12/2/18 - 18/2/18, 19/2/18 - 25/2/ 18, 26/2/18 - 4/3/18, 5/3/18 - 11/3/18, 12/3/18 - 18/3/18, 19/3/18 - 25/3/18, 26/3/18 - 1/4/18, 2/4/18 - 8/4/18, 9/4/18 - 15/4/18, 16/4/18 - 22/4/18, 23/ 4/18 - 29/4/18, 30/4/18 - 6/5/18, 7/5/18 - 13/5/18, 14/5/18 - 20/5/18, 21/5/ 18 - 27/5/18, 28/5/18 - 3/6/18, 4/6/18 - 10/6/18, 11/6/18 - 17/6/18, 18/6/18 - 24/6/18、25/6/18 - 1/7/18、2/7/18 - 8/7/18、9/7/18 - 15/7/18、16/7/18 - 22/ 7/18, 23/7/18 - 29/7/18, 30/7/18 - 5/8/18, 6/8/18 - 12/8/18, 13/8/18 - 19/8/ 18, 20/8/18 - 26/8/18, 27/8/18 - 2/9/18, 3/9/18 - 9/9/18, 10/9/18 - 16/9/18, 17/9/18 - 23/9/18, 24/9/18 - 30/9/18, 1/10/18 - 7/10/18, 8/10/18 - 14/10/18, 15/ 10/18 - 21/10/18, 22/10/18 - 28/10/18, 29/10/18 - 4/11/18, 5/11/18 - 11/11/18, 12/11/ 18 - 18/11/18, 19/11/18 - 25/11/18, 26/11/18 - 2/12/18, 3/12/18 - 9/12/18, 10/12/18 - 16/12/18, 17/12/18 - 23/12/18, 24/12/18 - 30/12/18, 31/12/ 18 - 6/1/19]

更合理的做法是提取格式化程序和 Year

Locale locale = Locale.forLanguageTag( "es-ES" ); // Spain locale.
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( locale );

Year year = Year.of( 2018 );
List < String > weeks =
        year
                .atDay( 1 )
                .with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) )
                .datesUntil( year.plusYears( 1 ).atDay( 1 ) , Period.ofWeeks( 1 ) )
                .map( localDate -> localDate.format( formatter ) + " - " + localDate.plusDays( 6 ).format( formatter ) )
                .toList();  // Before Java 16, change to .collect( Collectors.toList()); or .collect( Collectors.toUnmodifiableList());

ISO 8601

如果您对周的定义符合 ISO 8601 definition of week

  • 一周的第一天是星期一
  • 一年中的第 1 周包含日历年的第一个星期四

... 那么我建议将 ThreeTen-Extra 库添加到您的项目中以利用 YearWeek 类。

答案 1 :(得分:0)

公共课周{

public static void main(String[] args) {
    int year = 2018;
    int weeks = getNumWeeksForYear(year);

    for (int i = 1; i < weeks; i++) {

        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.WEEK_OF_YEAR, i);
        calendar.set(Calendar.YEAR, year);

        Date monday = calendar.getTime();
        calendar.add(Calendar.DATE, 6);
        Date sunday = calendar.getTime();
        System.out.println("week" + i);
        System.out.println(monday);
        System.out.println(sunday);
    }

}

public static int getNumWeeksForYear(int year) {
    Calendar c = Calendar.getInstance();
    c.set(year, 0, 1);
    return c.getMaximum(Calendar.WEEK_OF_YEAR);
}

}