获取一年日期的工具

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

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&lt;LocalDate&gt; getDaysOfYear(int year) {
	// initialize a list of LocalDate
	List&lt;LocalDate&gt; yearDates = new ArrayList&lt;&gt;();
	/*
	 * 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 &lt;= 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&lt;LocalDate&gt; 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(&quot;uuuu-MM-dd&quot;,
																	localeToBeUsed))
				+ &quot;, &quot; + dayOfWeek.getDisplayName(TextStyle.FULL, localeToBeUsed)
				+ &quot;, CW &quot; + calendarWeek
				+ &quot;, &quot; + 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

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:

确定