Java Util获取一年的日期

Java Util获取一年的日期,java,Java,我有一个表,其中我希望每一行都表示为一个日期,还有一些其他列表示该特定日期的特征。所以,基本上我一年要排365行。我需要用Java编写一个批处理作业,通过rest端点触发。我会把某一年交给财务总监(如2020年)。然后,我希望有一种方法,可以让我在2020年拥有366天(因为2020年是闰年)以及周末(周六/周日)或周日(周一至周五)。 我稍后会将这366天的数据批量插入数据库 有人能帮我写这个实用方法吗。要接收给定年份的日期列表,可以使用java.time创建如下方法: public sta

我有一个表,其中我希望每一行都表示为一个日期,还有一些其他列表示该特定日期的特征。所以,基本上我一年要排365行。我需要用Java编写一个批处理作业,通过rest端点触发。我会把某一年交给财务总监(如2020年)。然后,我希望有一种方法,可以让我在2020年拥有366天(因为2020年是闰年)以及周末(周六/周日)或周日(周一至周五)。

我稍后会将这366天的数据批量插入数据库


有人能帮我写这个实用方法吗。

要接收给定年份的日期列表,可以使用
java.time创建如下方法:

public static List<LocalDate> getDaysOfYear(int year) {
    // initialize a list of LocalDate
    List<LocalDate> yearDates = new ArrayList<>();
    /*
     * create a year object from the argument
     * to reliably get the amount of days that year has
     */
    Year thatYear = Year.of(year);
    // then just add a LocalDate per day of that year to the list
    for (int dayOfYear = 1; dayOfYear <= thatYear.length(); dayOfYear++) {
        yearDates.add(LocalDate.ofYearDay(year, dayOfYear));
    }
    // and return the list
    return yearDates;
}
输出将如下所示(为简洁起见,仅显示部分行):

2020-01-01,1月1日星期三
...
2020-02-29,2月9日,星期六
...
2020年5月19日,星期五
...
2020-12-31,12月1日,星期四

2020年有366天。。。你考虑过闰年吗?到目前为止,您(在Java中)尝试了什么?你能给我们看看吗?我已经更新了我的问题,把它改为366。更轻松的一点是,我只是把它作为一个例子,同样的情况也适用于我所附的表格。请不要看里面的数据,只是一个傀儡。是的,对于非leap,我会有365天,对于leap,我会有366天。不,我不知道那个特殊的实用方法。我可以向您展示控制器和JDBC批上传代码的其余部分,但不确定这是否有帮助。任何API或任何东西的帮助都会很感激。实用程序方法的签名看起来怎么样?某种程度上类似于
公共列表getDaysOfYear(int year)
?是的,您是。。。您可以提取星期几、月份、相应的日历周等
java.time
值得一看,因为它是java的现代内置日期和时间API。
public static void main(String[] args) {
    // receive the LocalDates of a given year
    List<LocalDate> yearDates = getDaysOfYear(2020);
    // define a locale for output (language, formats and so on)
    Locale localeToBeUsed = Locale.US;
    
    // then extract information about each date
    for (LocalDate date : yearDates) {
        // or extract the desired parts, like the day of week
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        // the month
        Month month = date.getMonth();
        // the calendar week based on a locale (the one of your system here)
        WeekFields weekFields = WeekFields.of(localeToBeUsed);
        int calendarWeek = date.get(weekFields.weekOfWeekBasedYear());
        // and print the concatenated information (formatted, depending on the locale)
        System.out.println(date.format(DateTimeFormatter.ofPattern("uuuu-MM-dd",
                                                                    localeToBeUsed))
                + ", " + dayOfWeek.getDisplayName(TextStyle.FULL, localeToBeUsed)
                + ", CW " + calendarWeek
                + ", " + month.getDisplayName(TextStyle.FULL, localeToBeUsed));
    }
}