This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021″

❤️ About the author: Hello everyone, I am Xiao Xu Zhu. Java field quality creator 🏆, CSDN blog expert certification 🏆, Huawei Cloud enjoy expert certification 🏆

❤️ technology live, the appreciation

❤️ like 👍 collect ⭐ look again, form a habit

Before reading this article, it is recommended to have some knowledge of the date and time of Java source code. If not, you can read this article first:

Swastika blog teaches you to understand Java source code date and time related usage

Related articles:

Hutool actual combat (take you to master the various tools) directory

3HUtool combat :DateUtil- Get various contents of the date


Source code analysis purposes

Know what it is and why

Project reference

The basis of this blog: Hutool -5.6.5 version of the source code

        <dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-core</artifactId>
			<version>5.6.5</version>
		</dependency>
Copy the code

Dateutil. year(java.util.date)

Methods described

Get the year portion

Source code Analysis 1

	/** * gets the part of the year **@paramThe date date *@returnPart of year */
	public static int year(Date date) {
		return DateTime.of(date).year();
	}
Copy the code

** datetime.of (date).year()** can be split into two parts

  • DateTime.of(date)
  • DateTime.year()

The first part, which is easy to understand from the code, is the type conversion

/** * convert JDK date to DateTime **@param date JDK Date
	 * @return DateTime
	 */
	public static DateTime of(Date date) {
		if (date instanceof DateTime) {
			return (DateTime) date;
		}
		return new DateTime(date);
	}
Copy the code

Part two,

/** * gets the part of the year **@returnPart of year */
public int year(a) {
   return getField(DateField.YEAR);
}

/** * to get a part of a date <br> * For example, to get a part of a YEAR, use getField(datePart.year) **@paramField represents an enumeration of what part of the date {@link DateField}
	 * @returnThe value of some part */
	public int getField(DateField field) {
		return getField(field.getValue());
	}

/** * to get a part of the date <br> * For example, to get a part of the YEAR, use getField(calendar.year) **@paramField represents what part of the date's int value {@link Calendar}
	 * @returnThe value of some part */
	public int getField(int field) {
		return toCalendar().get(field);
	}
Copy the code

Following the code, you can see that toCalendar().get(field)

/** * convert to Calendar, default {@link Locale}
 *
 * @return {@link Calendar}
 */
public Calendar toCalendar(a) {
   return toCalendar(Locale.getDefault(Locale.Category.FORMAT));
}
Copy the code

**toCalendar()** will get Calendar, which should make it easier to get the year.

You can get not just years, but years, months, days, hours, minutes, seconds. For more details, see here: The Swastika blog teaches you to understand the date and time related usage of Java source code

Dateutil.quarter (java.util.date) dateutil.quarter (java.util.date)

Methods described

Gets the quarter for the specified date, counting from 1

Source code Analysis 1

/** * gets the quarter of the specified date, counting from 1 **@paramThe date date *@returnWhich quarter *@since4.1.0 * /
public static int quarter(Date date) {
   return DateTime.of(date).quarter();
}
Copy the code

Datetime.of (date).quarter()** can be split into two parts

  • DateTime.of(date)
  • DateTime.quarter()

DateTime. Of (date).

In the second part, month() takes the month for the DateTime and does a simple calculation.

/** * gets the quarter of the current date, counting from 1 <br> **@returnWhich quarter {@link Quarter}
 */
public int quarter(a) {
   return month() / 3 + 1;
}

/** * get the month, counting from 0 **@returnIn the * /
	public int month(a) {
		return getField(DateField.MONTH);
	}
/** * to get a part of a date <br> * For example, to get a part of a YEAR, use getField(datePart.year) **@paramField represents an enumeration of what part of the date {@link DateField}
	 * @returnThe value of some part */
	public int getField(DateField field) {
		return getField(field.getValue());
	}
Copy the code

The getField method is explained above, so I won’t bother with the word here.

Dateutil.quarterenum (java.util.date)

Methods described

Gets the Quarter of the specified date, returning Quarter

Source code Analysis 1

/** * get the quarter of the specified date **@paramThe date date *@returnEnumeration of quarters *@since4.1.0 * /
public static Quarter quarterEnum(Date date) {
   return DateTime.of(date).quarterEnum();
}
Copy the code

Source ** datetime.of (date).quarterenum ()** can be split into two parts

  • DateTime.of(date)
  • DateTime.quarterEnum()

DateTime. Of (date).

In the second part, quarter(), there is source code analysis. The conclusion is to obtain the current date of the quarter, counting from 1

**Quarter. Of (int)** converts the corresponding value into the corresponding enumeration Quarter

/** * get the quarter of the current date <br> **@returnWhich quarter {@link Quarter}
 */
public Quarter quarterEnum(a) {
   return Quarter.of(quarter());
}
Copy the code

Dateutil.month (java.util.date) dateutil.month (java.util.date)

Methods described

Gets the months, counting from 0

Source code Analysis 1

/** * get the month, counting from 0 **@paramThe date date *@returnMonth, counting from 0 */
public static int month(Date date) {
   return DateTime.of(date).month();
}
Copy the code

Datetime.of (date).month()** can be split into two parts

  • DateTime.of(date)
  • DateTime.month()

DateTime. Of (date).

The second part, the getField method, is explained above, so there is no water word here.

/** * get the month, counting from 0 **@returnIn the * /
public int month(a) {
   return getField(DateField.MONTH);
}
Copy the code

Dateutil.monthenum (java.util.date)

Methods described

Gets the Month and returns the Month enumeration object Month

Source code Analysis 1

/** * get month **@paramThe date date *@return {@link Month}
 */
public static Month monthEnum(Date date) {
   return DateTime.of(date).monthEnum();
}
Copy the code

** datetime.of (date).monthenum ()** can be split into two parts

  • DateTime.of(date)
  • DateTime.monthEnum()

DateTime. Of (date).

In the second part, month() gets the months, counting from 0

** month. of(int)** converts the corresponding value into the corresponding enumeration Month

/** * get month **@return {@link Month}
 */
public Month monthEnum(a) {
   return Month.of(month());
}
Copy the code

Dateutil.weekofyear (java.util.date)

Methods described

This method returns a value relative to the first day of the week, for example: If the first day of the week is Monday, it is the first week. (Return 1) < BR > The result for the week of the New Year is always 1

Source code Analysis 1

<br> * This method returns the first day of the week, for example: <br> * January 3, 2016 is Sunday, if the first day of the week is Sunday, it is the second week (return 2) < BR > * If the first day of the week is Monday, it is the first week (return 1) < BR > * The week of the New Year is always 1 * *@paramThe date date *@returnWeeks *@see DateTime#setFirstDayOfWeek(Week)
 */
public static int weekOfYear(Date date) {
   return DateTime.of(date).weekOfYear();
}
Copy the code

** datetime.of (date).weekofyear ()** can be split into two parts

  • DateTime.of(date)
  • DateTime.weekOfYear()

DateTime. Of (date).

The second part, the getField method, is explained above, so there is no water word here.

<br> * This method returns the first day of the week, for example: <br> * January 3, 2016 is Sunday, if the first day of the week is Sunday, it is the second week (return 2) < BR > * If the first day of the week is Monday, it is the first week (return 1) < BR > * The week of the New Year is always 1 * *@returnWeeks *@see #setFirstDayOfWeek(Week)
 */
public int weekOfYear(a) {
   return getField(DateField.WEEK_OF_YEAR);
}
Copy the code

Dateutil.weekofmonth (java.util.date)

Methods described

Gets the week of the month in which the specified date is <br>

Source code Analysis 1

/** * gets the day of the month in which the specified date is <br> **@paramThe date date *@returnDay * /
public static int dayOfMonth(Date date) {
   return DateTime.of(date).dayOfMonth();
}
Copy the code

** datetime.of (date).year()** can be split into two parts

  • DateTime.of(date)
  • DateTime.monthEnum()

DateTime. Of (date).

The second part, the getField method, is explained above, so there is no water word here.

/** * returns the day of the month in which the specified date is located, starting at 1 **@returnDay. 1 indicates the first day */
public int dayOfMonth(a) {
   return getField(DateField.DAY_OF_MONTH);
}
Copy the code

Dateutil.dayofmonth (java.util.date)

Methods described

Gets the day of the month in which the specified date is located

Source code Analysis 1

/** * gets the day of the month in which the specified date is <br> **@paramThe date date *@returnDay * /
public static int dayOfMonth(Date date) {
   return DateTime.of(date).dayOfMonth();
}
Copy the code

Datetime.of (date).dayofmonth ()** can be split into two parts

  • DateTime.of(date)
  • DateTime.dayOfMonth()

DateTime. Of (date).

The second part, the getField method, is explained above, so there is no water word here.

/** * returns the day of the month in which the specified date is located, starting at 1 **@returnDay. 1 indicates the first day */
public int dayOfMonth(a) {
   return getField(DateField.DAY_OF_MONTH);
}
Copy the code

Dateutil.dayofyear (java.util.date)

Methods described

The day of the year in which the specified date is obtained

Source code Analysis 1

/** * the day of the year in which the specified date is obtained **@paramThe date date *@returnDay *@since5.3.6 * /
public static int dayOfYear(Date date) {
   return DateTime.of(date).dayOfYear();
}
Copy the code

Datetime.of (date).dayofyear ()** can be split into two parts

  • DateTime.of(date)
  • DateTime.dayOfYear()

DateTime. Of (date).

The second part, the getField method, is explained above, so there is no water word here.

/** * returns the day of the year in which the specified date is located, starting at 1 **@returnDay, 1 indicates the first day *@since5.3.6 * /
public int dayOfYear(a) {
   return getField(DateField.DAY_OF_YEAR);
}
Copy the code

Dateutil. dayOfWeek(java.util.date)

Methods described

Get the specified day of the week, 1 for Sunday and 2 for Monday

Source code Analysis 1

/** * returns the day of the week. 1 indicates Sunday, and 2 indicates Monday@paramThe date date *@returnDay * /
public static int dayOfWeek(Date date) {
   return DateTime.of(date).dayOfWeek();
}
Copy the code

Datetime.of (date).dayofweek ()** can be split into two parts

  • DateTime.of(date)
  • DateTime.dayOfWeek()

DateTime. Of (date).

The second part, the getField method, is explained above, so there is no water word here.

/** * returns the day of the week. 1 indicates Sunday, and 2 indicates Monday@return* /
public int dayOfWeek(a) {
   return getField(DateField.DAY_OF_WEEK);
}
Copy the code

Dateutil. dayOfWeekEnum(java.util.date)

Methods described

Get the specified day of the week

Source code Analysis 1

/** * get the specified day of the week **@paramThe date date *@return {@link Week}
 */
public static Week dayOfWeekEnum(Date date) {
   return DateTime.of(date).dayOfWeekEnum();
}
Copy the code

Datetime.of (date).dayofweekenum ()** can be split into two parts

  • DateTime.of(date)
  • DateTime.dayOfWeekEnum()

DateTime. Of (date).

The second part, dayOfWeek(), gets the specified day of the week, with 1 indicating Sunday and 2 indicating Monday

** week. of(int)** converts the corresponding value into the corresponding enumeration Week

/** * get the specified day of the week **@return {@link Week}
 */
public Week dayOfWeekEnum(a) {
   return Week.of(dayOfWeek());
}
Copy the code

Dateutil. hour(java.util.Date, Boolean)

Methods described

Get the number of hours for the specified date.

Source code Analysis 1

/** * gets the number of hours of the specified date@paramThe date date *@paramIs24HourClock Specifies whether the clock is in 24-hour mode *@returnHours * /
public static int hour(Date date, boolean is24HourClock) {
   return DateTime.of(date).hour(is24HourClock);
}
Copy the code

** datetime.of (date).hour(is24HourClock)** can be split into two parts

  • DateTime.of(date)
  • DateTime.hour(is24HourClock)

DateTime. Of (date).

The second part, the getField method, is explained above, so there is no water word here.

is24HourClock ? HOUR_OF_DAY: datefield. HOUR: is24HourClock Specifies whether the hourclock is set to 24 hours

Datefield. HOUR_OF_DAY: hour, used in the 24-hour system

Datefield. HOUR: indicates the HOUR in the 12-hour system

/** * gets the number of hours of the specified date@paramIs24HourClock Specifies whether the clock is in 24-hour mode *@returnHours * /
public int hour(boolean is24HourClock) {
   return getField(is24HourClock ? DateField.HOUR_OF_DAY : DateField.HOUR);
}
Copy the code

Dateutil. minute(java.util.date)

Methods described

Get the number of minutes for the specified date < BR > for example: 10:04:15.250 = 4

Source code Analysis 1

/** ** gets the number of minutes for the specified date <br> * example: 10:04:15.250 = "4 **"@paramThe date date *@returnMinutes * /
public static int minute(Date date) {
   return DateTime.of(date).minute();
}
Copy the code

** datetime.of (date).minute()** can be split into two parts

  • DateTime.of(date)
  • DateTime.minute()

DateTime. Of (date).

The second part, the getField method, is explained above, so there is no water word here.

/** ** gets the number of minutes for the specified date <br> * example: 10:04:15.250 = "4 **"@returnMinutes * /
public int minute(a) {
   return getField(DateField.MINUTE);
}
Copy the code

Dateutil. second(java.util.date)

Methods described

Part of the number of seconds to get the specified date

Source code Analysis 1

/** * gets the number of seconds part of the specified date@paramThe date date *@returnThe number of seconds * /
public static int second(Date date) {
   return DateTime.of(date).second();
}
Copy the code

** datetime.of (date).second()** can be split into two parts

  • DateTime.of(date)
  • DateTime.second()

DateTime. Of (date).

The second part, the getField method, is explained above, so there is no water word here.

/** * gets the number of seconds part of the specified date@returnThe number of seconds * /
public int second(a) {
   return getField(DateField.SECOND);
}
Copy the code

Dateutil.millisecond (java.util.date)

Methods described

Retrieves the number of milliseconds for the specified date.

Source code Analysis 1

	/** * retrieves the milliseconds portion of the specified date@paramThe date date *@returnMilliseconds * /
	public static int millisecond(Date date) {
		return DateTime.of(date).millisecond();
	}
Copy the code

Datetime.of (date).millisecond()** datetime.of (date).millisecond()**

  • DateTime.of(date)
  • DateTime.millisecond()

DateTime. Of (date).

The second part, the getField method, is explained above, so there is no water word here.

/** * retrieves the milliseconds portion of the specified date@returnMilliseconds * /
public int millisecond(a) {
   return getField(DateField.MILLISECOND);
}
Copy the code

Dateutil. isAM(java.util.date)

Methods described

Morning or not

Source code Analysis 1

/** * Whether is morning **@paramThe date date *@returnWhether it is in the morning */
public static boolean isAM(Date date) {
   return DateTime.of(date).isAM();
}
Copy the code

** datetime.of (date).isam ()** can be split into two parts

  • DateTime.of(date)
  • DateTime.isAM()

DateTime. Of (date).

The second part, the getField method, is explained above, so there is no water word here.

Calendar.AM == getField(datefield.am_pm)

/** * Whether is morning **@returnWhether it is in the morning */
public boolean isAM(a) {
   return Calendar.AM == getField(DateField.AM_PM);
}
Copy the code

Dateutil.ispm (java.util.date)

Methods described

Is it afternoon?

Source code Analysis 1

/** * whether is afternoon **@paramThe date date *@returnIs it afternoon */
public static boolean isPM(Date date) {
   return DateTime.of(date).isPM();
}
Copy the code

** datetime.of (date).ispm ()** can be split into two parts

  • DateTime.of(date)
  • DateTime.isPM()

DateTime. Of (date).

The second part, the getField method, is explained above, so there is no water word here.

Calendar.PM== getField(datefield.am_pm)

Public Boolean isPM() {return calendar.pm == getField(datefield.am_pm); }Copy the code

Method name: dateutil.thisyear ()

Methods described

Return this year

Source code Analysis 1

/ * * *@returnThis year * /
public static int thisYear(a) {
   return year(date());
}
Copy the code

**year(date())** can be split into two parts

  • Datetime.date () gets the current time, returning DateTime
  • Datetime.year () gets a partial source analysis of the year

DateTime.date()

/** * current time, convert to {@linkDateTime} object * *@returnCurrent time */
public static DateTime date(a) {
   return new DateTime();
}
Copy the code

Method name: dateutil.thisMonth ()

Methods described

Return current month

Source code Analysis 1

/ * * *@returnCurrent month */
public static int thisMonth(a) {
   return month(date());
}
Copy the code

Source code **month(date())** can be split into two parts

  • Datetime.date () gets the current time, returning DateTime
  • Datetime.month () gets a partial source analysis of the year

DateTime.date()

/** * current time, convert to {@linkDateTime} object * *@returnCurrent time */
public static DateTime date(a) {
   return new DateTime();
}
Copy the code

Method name: dateutil.thismonthenum ()

Methods described

Return current month

Source code Analysis 1

/ * * *@returnCurrent month {@link Month}
 */
public static Month thisMonthEnum(a) {
   return monthEnum(date());
}
Copy the code

Source code **monthEnum(date())** can be split into two parts

  • Datetime.date () gets the current time, returning DateTime
  • Datetime.monthenum () gets a partial source code analysis of the year

DateTime.date()

/** * current time, convert to {@linkDateTime} object * *@returnCurrent time */
public static DateTime date(a) {
   return new DateTime();
}
Copy the code

Dateutil.thisweekofyear ()

Methods described

Returns the week of the year of the current date

Source code Analysis 1

/ * * *@returnWeek in the year of the current date */
public static int thisWeekOfYear(a) {
   return weekOfYear(date());
}
Copy the code

**weekOfYear(date())** can be split into two parts

  • Datetime.date () gets the current time, returning DateTime
  • Datetime.weekofyear () gets a partial source analysis of the year

DateTime.date()

/** * current time, convert to {@linkDateTime} object * *@returnCurrent time */
public static DateTime date(a) {
   return new DateTime();
}
Copy the code

Method name: DateUtil thisWeekOfMonth ()

Methods described

Returns the week in the month of the current date

Source code Analysis 1

	/ * * *@returnWeek of the month in which the current date is */
	public static int thisWeekOfMonth(a) {
		return weekOfMonth(date());
	}
Copy the code

**weekOfMonth(date())** can be split into two parts

  • Datetime.date () gets the current time, returning DateTime
  • Datetime.weekofmonth () gets a partial source code analysis of the year

DateTime.date()

/** * current time, convert to {@linkDateTime} object * *@returnCurrent time */
public static DateTime date(a) {
   return new DateTime();
}
Copy the code

Dateutil.thisdayofmonth ()

Methods described

Returns the day of the month in which the current date is

Source code Analysis 1

	/ * * *@returnThe current date is the day of the month in which the date is */
	public static int thisDayOfMonth(a) {
		return dayOfMonth(date());
	}
Copy the code

Source code **dayOfMonth(date())** can be split into two parts

  • Datetime.date () gets the current time, returning DateTime
  • Datetime.dayofmonth () gets a partial source code analysis of the year

DateTime.date()

/** * current time, convert to {@linkDateTime} object * *@returnCurrent time */
public static DateTime date(a) {
   return new DateTime();
}
Copy the code

Method name: dateutil.thisdayofweek ()

Methods described

Returns what day of the week the current date is

Source code Analysis 1

	/ * * *@returnWhat day of the week is the current date */
	public static int thisDayOfWeek(a) {
		return dayOfWeek(date());
	}
Copy the code

Source code **dayOfWeek(date())** can be split into two parts

  • Datetime.date () gets the current time, returning DateTime
  • Datetime.dayofweek () gets a partial source code analysis of the year

DateTime.date()

/** * current time, convert to {@linkDateTime} object * *@returnCurrent time */
public static DateTime date(a) {
   return new DateTime();
}
Copy the code

Method name: DateUtil thisDayOfWeekEnum ()

Methods described

Returns what day of the week the current date is

Source code Analysis 1

	/ * * *@returnWhat day of the week is the current date?@link Week}
	 */
	public static Week thisDayOfWeekEnum(a) {
		return dayOfWeekEnum(date());
	}
Copy the code

**dayOfWeekEnum(date())** can be split into two parts

  • Datetime.date () gets the current time, returning DateTime
  • Datetime.dayofweekenum () gets a partial source code analysis of the year

DateTime.date()

/** * current time, convert to {@linkDateTime} object * *@returnCurrent time */
public static DateTime date(a) {
   return new DateTime();
}
Copy the code

Method name: dateutil. thisHour(Boolean)

Methods described

Returns the number of hours of the current date

Source code Analysis 1

	/ * * *@paramIs24HourClock Specifies whether the clock is in 24-hour mode *@returnThe hour portion of the current date <br> */
	public static int thisHour(boolean is24HourClock) {
		return hour(date(), is24HourClock);
	}
Copy the code

Source code **hour(date(), is24HourClock)** can be split into two parts

  • Datetime.date () gets the current time, returning DateTime
  • Datetime.hour () gets a partial source code analysis of the year

DateTime.date()

/** * current time, convert to {@linkDateTime} object * *@returnCurrent time */
public static DateTime date(a) {
   return new DateTime();
}
Copy the code

Method name: dateutil.thisminute ()

Methods described

Returns the minutes portion of the current date

Source code Analysis 1

	/ * * *@returnThe minute portion of the current date <br> */
	public static int thisMinute(a) {
		return minute(date());
	}
Copy the code

Source **minute(date())** can be split into two parts

  • Datetime.date () gets the current time, returning DateTime
  • Datetime.minute () gets a partial source code analysis of the year

DateTime.date()

/** * current time, convert to {@linkDateTime} object * *@returnCurrent time */
public static DateTime date(a) {
   return new DateTime();
}
Copy the code

Method name: dateutil.thisSecond ()

Methods described

Returns the number of seconds portion of the current date

Source code Analysis 1

	/ * * *@returnThe number of seconds of the current date <br> */
	public static int thisSecond(a) {
		return second(date());
	}
Copy the code

Source code **second(date())** can be split into two parts

  • Datetime.date () gets the current time, returning DateTime
  • Datetime.second () gets a partial source analysis of the year

DateTime.date()

/** * current time, convert to {@linkDateTime} object * *@returnCurrent time */
public static DateTime date(a) {
   return new DateTime();
}
Copy the code

Method name: DateUtil thisMillisecond ()

Methods described

Returns the number of milliseconds of the current date

Source code Analysis 1

	/ * * *@returnThe number of milliseconds part of the current date <br> */
	public static int thisMillisecond(a) {
		return millisecond(date());
	}
Copy the code

Source version **millisecond(date())** is deprecated into two parts

  • Datetime.date () gets the current time, returning DateTime
  • Datetime.millisecond () returns partial source code analysis of year

DateTime.date()

/** * current time, convert to {@linkDateTime} object * *@returnCurrent time */
public static DateTime date(a) {
   return new DateTime();
}
Copy the code

Dateutil. yearAndQuarter(java.util.date)

Methods described

Get the specified date year and season <br> format: [20131] represents the first quarter of 2013

Source code Analysis 1

/** * get the specified date, year and season <br> * format: [20131] represents the first quarter of 2013 **@paramThe date date *@returnQuarter, similar to 20132 */
public static String yearAndQuarter(Date date) {
   return yearAndQuarter(calendar(date));
}
Copy the code

Source **yearAndQuarter(Calendar (date))** can be split into two parts

  • Calendar (Date): The date object is converted to a Calendar object
  • yearAndQuarter(Calendar)

YearAndQuarter method code analysis:

Cal.get (calendar.year)

Get (calendar.month) / 3 + 1

The string is then concatenated through StringBuilder

/** * get the specified date, year and quarter <br> * format: [20131] represents the first quarter of 2013 **@paramCAL date *@returnThe year and quarter format is similar to 20131 */
public static String yearAndQuarter(Calendar cal) {
   return StrUtil.builder().append(cal.get(Calendar.YEAR)).append(cal.get(Calendar.MONTH) / 3 + 1).toString();
}
Copy the code

Dateutil. yearAndQuarter(java.util.Date, java.util.date)

Methods described

Gets the year and season within the specified date range

Source code Analysis 1

/** * gets the year and season within the specified date range@paramStartDate startDate (inclusive) *@paramEndDate endDate (inclusive) *@returnQuarterly list with elements similar to 20132 */
public static LinkedHashSet<String> yearAndQuarter(Date startDate, Date endDate) {
   if (startDate == null || endDate == null) {
      return new LinkedHashSet<>(0);
   }
   return yearAndQuarter(startDate.getTime(), endDate.getTime());
}
Copy the code

As shown in the code above, there is a null-handling

Next, we write a while loop that stores the eligible year and quarter in LinkedHashSet. After storing a year and quarter string, we add 3 months to the start time. If the start time exceeds the end time, we set the end time to the start time and end the loop

/** * gets the year and quarter within the specified date range@paramStartDate startDate (inclusive) *@paramEndDate endDate (inclusive) *@returnQuarterly list with elements similar to 20132 *@since4.1.15 * /
public static LinkedHashSet<String> yearAndQuarter(long startDate, long endDate) {
   LinkedHashSet<String> quarters = new LinkedHashSet<>();
   final Calendar cal = calendar(startDate);
   while (startDate <= endDate) {
      // If the start time exceeds the end time, set the end time to the start time and end the loop after processing
      quarters.add(yearAndQuarter(cal));

      cal.add(Calendar.MONTH, 3);
      startDate = cal.getTimeInMillis();
   }

   return quarters;
}
Copy the code