获取一年日期的工具

huangapple go评论157阅读模式
英文:

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创建类似以下的方法:

  1. public static List<LocalDate> getDaysOfYear(int year) {
  2. // 初始化一个LocalDate列表
  3. List<LocalDate> yearDates = new ArrayList<>();
  4. /*
  5. * 从参数创建一个年份对象
  6. * 以可靠地获取该年的天数
  7. */
  8. Year thatYear = Year.of(year);
  9. // 然后将该年的每一天添加到列表中的一个LocalDate
  10. for (int dayOfYear = 1; dayOfYear <= thatYear.length(); dayOfYear++) {
  11. yearDates.add(LocalDate.ofYearDay(year, dayOfYear));
  12. }
  13. // 然后返回列表
  14. return yearDates;
  15. }

您可以使用结果来提取有关每一天的信息(例如,在main方法中):

  1. public static void main(String[] args) {
  2. // 获取给定年份的LocalDate列表
  3. List<LocalDate> yearDates = getDaysOfYear(2020);
  4. // 定义用于输出的区域设置(语言、格式等)
  5. Locale localeToBeUsed = Locale.US;
  6. // 然后提取每个日期的信息
  7. for (LocalDate date : yearDates) {
  8. // 或提取所需的部分,如星期几
  9. DayOfWeek dayOfWeek = date.getDayOfWeek();
  10. // 月份
  11. Month month = date.getMonth();
  12. // 基于区域设置的日历周(此处为系统的区域设置)
  13. WeekFields weekFields = WeekFields.of(localeToBeUsed);
  14. int calendarWeek = date.get(weekFields.weekOfWeekBasedYear());
  15. // 并打印连接的信息(根据区域设置进行格式化)
  16. System.out.println(date.format(DateTimeFormatter.ofPattern("uuuu-MM-dd",
  17. localeToBeUsed))
  18. + ", " + dayOfWeek.getDisplayName(TextStyle.FULL, localeToBeUsed)
  19. + ", CW " + calendarWeek
  20. + ", " + month.getDisplayName(TextStyle.FULL, localeToBeUsed));
  21. }
  22. }

输出将如下所示(仅为简洁起见的一些行):

  1. 2020-01-01, Wednesday, CW 1, January
  2. ...
  3. 2020-02-29, Saturday, CW 9, February
  4. ...
  5. 2020-05-08, Friday, CW 19, May
  6. ...
  7. 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:

  1. public static List&lt;LocalDate&gt; getDaysOfYear(int year) {
  2. // initialize a list of LocalDate
  3. List&lt;LocalDate&gt; yearDates = new ArrayList&lt;&gt;();
  4. /*
  5. * create a year object from the argument
  6. * to reliably get the amount of days that year has
  7. */
  8. Year thatYear = Year.of(year);
  9. // then just add a LocalDate per day of that year to the list
  10. for (int dayOfYear = 1; dayOfYear &lt;= thatYear.length(); dayOfYear++) {
  11. yearDates.add(LocalDate.ofYearDay(year, dayOfYear));
  12. }
  13. // and return the list
  14. return yearDates;
  15. }

You can use the result in order to extract information about each day (in a main for example):

  1. public static void main(String[] args) {
  2. // receive the LocalDates of a given year
  3. List&lt;LocalDate&gt; yearDates = getDaysOfYear(2020);
  4. // define a locale for output (language, formats and so on)
  5. Locale localeToBeUsed = Locale.US;
  6. // then extract information about each date
  7. for (LocalDate date : yearDates) {
  8. // or extract the desired parts, like the day of week
  9. DayOfWeek dayOfWeek = date.getDayOfWeek();
  10. // the month
  11. Month month = date.getMonth();
  12. // the calendar week based on a locale (the one of your system here)
  13. WeekFields weekFields = WeekFields.of(localeToBeUsed);
  14. int calendarWeek = date.get(weekFields.weekOfWeekBasedYear());
  15. // and print the concatenated information (formatted, depending on the locale)
  16. System.out.println(date.format(DateTimeFormatter.ofPattern(&quot;uuuu-MM-dd&quot;,
  17. localeToBeUsed))
  18. + &quot;, &quot; + dayOfWeek.getDisplayName(TextStyle.FULL, localeToBeUsed)
  19. + &quot;, CW &quot; + calendarWeek
  20. + &quot;, &quot; + month.getDisplayName(TextStyle.FULL, localeToBeUsed));
  21. }
  22. }

The output will look like this (just some of the lines for brevity):

  1. 2020-01-01, Wednesday, CW 1, January
  2. ...
  3. 2020-02-29, Saturday, CW 9, February
  4. ...
  5. 2020-05-08, Friday, CW 19, May
  6. ...
  7. 2020-12-31, Thursday, CW 1, December

huangapple
  • 本文由 发表于 2020年8月12日 16:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63372323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定