英文:
Util to get dates for a year
问题
I have a table where I would like each row to be represented as a date along with some other columns to represent the characteristic of that particular date. So, basically I would be having 365 rows for a year. I need to write a batch job in Java, which I would trigger through a rest endpoint. I would pass a particular year to the controller (eg 2020). I would then want a method which would get me all 366 days (since 2020 is a leap year) for 2020 along with the days i.e. weekends (Sat/Sun) or weekdays (Mon-Fri).
I would later do a batch insert into DB of those 366 days.
英文:
I have a table where I would like each row to be represented as a date along with some other columns to represent the characteristic of that particular date. So, basically I would be having 365 rows for a year. I need to write a batch job in Java, which I would trigger through a rest endpoint. I would pass a particular year to the controller (eg 2020). I would then want a method which would get me all 366 days (since 2020 is a leap year) for 2020 along with the days i.e. weekends (Sat/Sun) or weekdays (Mon-Fri).
I would later do a batch insert into DB of those 366 days.
Can someone help me write this utility method.
答案1
得分: 3
以下是翻译好的部分:
要获取给定年份的日期列表,您可以使用java.time
创建类似以下的方法:
public static List<LocalDate> getDaysOfYear(int year) {
// 初始化一个LocalDate列表
List<LocalDate> yearDates = new ArrayList<>();
/*
* 从参数创建一个年份对象
* 以可靠地获取该年的天数
*/
Year thatYear = Year.of(year);
// 然后将该年的每一天添加到列表中的一个LocalDate
for (int dayOfYear = 1; dayOfYear <= thatYear.length(); dayOfYear++) {
yearDates.add(LocalDate.ofYearDay(year, dayOfYear));
}
// 然后返回列表
return yearDates;
}
您可以使用结果来提取有关每一天的信息(例如,在main
方法中):
public static void main(String[] args) {
// 获取给定年份的LocalDate列表
List<LocalDate> yearDates = getDaysOfYear(2020);
// 定义用于输出的区域设置(语言、格式等)
Locale localeToBeUsed = Locale.US;
// 然后提取每个日期的信息
for (LocalDate date : yearDates) {
// 或提取所需的部分,如星期几
DayOfWeek dayOfWeek = date.getDayOfWeek();
// 月份
Month month = date.getMonth();
// 基于区域设置的日历周(此处为系统的区域设置)
WeekFields weekFields = WeekFields.of(localeToBeUsed);
int calendarWeek = date.get(weekFields.weekOfWeekBasedYear());
// 并打印连接的信息(根据区域设置进行格式化)
System.out.println(date.format(DateTimeFormatter.ofPattern("uuuu-MM-dd",
localeToBeUsed))
+ ", " + dayOfWeek.getDisplayName(TextStyle.FULL, localeToBeUsed)
+ ", CW " + calendarWeek
+ ", " + month.getDisplayName(TextStyle.FULL, localeToBeUsed));
}
}
输出将如下所示(仅为简洁起见的一些行):
2020-01-01, Wednesday, CW 1, January
...
2020-02-29, Saturday, CW 9, February
...
2020-05-08, Friday, CW 19, May
...
2020-12-31, Thursday, CW 1, December
英文:
To receive a list of dates of a given year, you can create a method like the following one using 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;
}
You can use the result in order to extract information about each day (in a main
for example):
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));
}
}
The output will look like this (just some of the lines for brevity):
2020-01-01, Wednesday, CW 1, January
...
2020-02-29, Saturday, CW 9, February
...
2020-05-08, Friday, CW 19, May
...
2020-12-31, Thursday, CW 1, December
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论