3hutool source code analysis: dateutil (time tool class) - obtain various contents of the date

Technical work should be rewarded
 follow+Click three times (like, comment, collect) and watch again to form a good habit

Before reading this article, I suggest you have a certain understanding of the date and time of the java source code. If you don't know, you can read this article first:

Wanzi blog teaches you to understand the date and time related usage of java source code

Related articles:

hutool actual combat (take you to master various tools inside) directory

3hutool actual combat: DateUtil - obtain various contents of the date

Source code analysis purpose

Know what it is, know why

Project reference

The basis of this blog post: hutool-5.6.5 version source code

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

Method name: dateutil year(java.util.Date)

Method description

Source code analysis I

	/**
	 * Get part of the year
	 *
	 * @param date date
	 * @return Part of the year
	 */
	public static int year(Date date) {
		return DateTime.of(date).year();
	}

Source code * * datetime of(date). Year (*) can be disassembled into two parts

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

/**
	 * 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);
	}

Part two,

/**
 * Get part of the year
 *
 * @return Part of the year
 */
public int year() {
   return getField(DateField.YEAR);
}

/**
	 * Get a part of the date < br >
	 * For example, to get the part of the year, use getField(DatePart.YEAR)
	 *
	 * @param field Enumeration indicating which part of the date {@ link DateField}
	 * @return Value of a part
	 */
	public int getField(DateField field) {
		return getField(field.getValue());
	}

/**
	 * Get a part of the date < br >
	 * For example, to get the year portion, use getField(Calendar.YEAR)
	 *
	 * @param field The int value {@ link Calendar}
	 * @return Value of a part
	 */
	public int getField(int field) {
		return toCalendar().get(field);
	}

/**
 * Convert to Calendar, default {@ link Locale}
 *
 * @return {@link Calendar}
 */
public Calendar toCalendar() {
   return toCalendar(Locale.getDefault(Locale.Category.FORMAT));
}

**toCalendar() * * you will get the Calendar, which is easy to understand. You can get the year.

You can get not only the year, but also the year, month, day, hour, minute and second. See here for details: Wanzi blog teaches you to understand the date and time related usage of java source code

Method name: dateutil quarter(java.util.Date)

Method description

Gets the quarter of the specified date, counting from 1

Source code analysis I

/**
 * Gets the quarter of the specified date, counting from 1
 *
 * @param date date
 * @return What quarter
 * @since 4.1.0
 */
public static int quarter(Date date) {
   return DateTime.of(date).quarter();
}

Source code * * datetime of(date). Quarter() * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

In the second part, month() gets the month corresponding to DateTime, and then makes a simple calculation.

/**
 * Obtain the quarter of the current date and count from 1 < br >
 *
 * @return What quarter {@ link Quarter}
 */
public int quarter() {
   return month() / 3 + 1;
}

/**
	 * Get month, count from 0
	 *
	 * @return month
	 */
	public int month() {
		return getField(DateField.MONTH);
	}
/**
	 * Get a part of the date < br >
	 * For example, to get the part of the year, use getField(DatePart.YEAR)
	 *
	 * @param field Enumeration indicating which part of the date {@ link DateField}
	 * @return Value of a part
	 */
	public int getField(DateField field) {
		return getField(field.getValue());
	}

getField The method is introduced above. There is no water here.

Method name: dateutil quarterEnum(java.util.Date)

Method description

Gets the Quarter to which the specified date belongs, and returns the Quarter enumeration object Quarter

Source code analysis I

/**
 * Get the quarter of the specified date
 *
 * @param date date
 * @return Quarter enumeration
 * @since 4.1.0
 */
public static Quarter quarterEnum(Date date) {
   return DateTime.of(date).quarterEnum();
}

Source code * * datetime of(date). Quarterenum() * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, quarter() There is a source code analysis above. The conclusion is to obtain the quarter of the current date and count from 1

**Quarter.of(int) * * is to convert the corresponding value into the corresponding enumeration quarter

/**
 * Obtain the quarter of the current date < br >
 *
 * @return What quarter {@ link Quarter}
 */
public Quarter quarterEnum() {
   return Quarter.of(quarter());
}

Method name: dateutil month(java.util.Date)

Method description

Source code analysis I

/**
 * Get month, count from 0
 *
 * @param date date
 * @return Month, counting from 0
 */
public static int month(Date date) {
   return DateTime.of(date).month();
}

Source code * * datetime of(date). Month() * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, getField The method is introduced above. There is no water here.

/**
 * Get month, count from 0
 *
 * @return month
 */
public int month() {
   return getField(DateField.MONTH);
}

Method name: dateutil monthEnum(java.util.Date)

Method description

Source code analysis I

/**
 * Get month
 *
 * @param date date
 * @return {@link Month}
 */
public static Month monthEnum(Date date) {
   return DateTime.of(date).monthEnum();
}

Source code * * datetime of(date). Monthenum() * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, month() Is to get the month, counting from 0

**Month.of(int) * * is to convert the corresponding numerical value into the corresponding enumeration month

/**
 * Get month
 *
 * @return {@link Month}
 */
public Month monthEnum() {
   return Month.of(month());
}

Method name: dateutil weekOfYear(java.util.Date)

Method description

Source code analysis I

/**
 * The specified date is the week of the year < br >
 * The return value of this method is related to the first day of the week, for example: < br >
 * 2016 January 3 is Sunday. If the first day of a week is Sunday, it is the second week (return 2) < br >
 * If the first day of a week is Monday, that day is the first week (return 1) < br >
 * The result of the week in the new year is always 1
 *
 * @param date date
 * @return week
 * @see DateTime#setFirstDayOfWeek(Week)
 */
public static int weekOfYear(Date date) {
   return DateTime.of(date).weekOfYear();
}

Source code * * datetime of(date). Weekofyear() * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, getField The method is introduced above. There is no water here.

/**
 * The specified date is the week of the year < br >
 * The return value of this method is related to the first day of the week, for example: < br >
 * 2016 January 3 is Sunday. If the first day of a week is Sunday, it is the second week (return 2) < br >
 * If the first day of a week is Monday, that day is the first week (return 1) < br >
 * The result of the week in the new year is always 1
 *
 * @return week
 * @see #setFirstDayOfWeek(Week)
 */
public int weekOfYear() {
   return getField(DateField.WEEK_OF_YEAR);
}

Method name: dateutil weekOfMonth(java.util.Date)

Method description

Source code analysis I

/**
 * The specified date is the day of the month in which the date is located < br >
 *
 * @param date date
 * @return day
 */
public static int dayOfMonth(Date date) {
   return DateTime.of(date).dayOfMonth();
}

Source code * * datetime of(date). Year (*) can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, getField The method is introduced above. There is no water here.

/**
 * The specified date is the day of the month in which the date is located, starting from 1
 *
 * @return Day, 1 means the first day
 */
public int dayOfMonth() {
   return getField(DateField.DAY_OF_MONTH);
}

Method name: dateutil dayOfMonth(java.util.Date)

Method description

Source code analysis I

/**
 * The specified date is the day of the month in which the date is located < br >
 *
 * @param date date
 * @return day
 */
public static int dayOfMonth(Date date) {
   return DateTime.of(date).dayOfMonth();
}

Source code * * datetime of(date). Dayofmonth() * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, getField The method is introduced above. There is no water here.

/**
 * The specified date is the day of the month in which the date is located, starting from 1
 *
 * @return Day, 1 means the first day
 */
public int dayOfMonth() {
   return getField(DateField.DAY_OF_MONTH);
}

Method name: dateutil dayOfYear(java.util.Date)

Method description

Source code analysis I

/**
 * Gets the day of the year in which the specified date is located
 *
 * @param date date
 * @return day
 * @since 5.3.6
 */
public static int dayOfYear(Date date) {
   return DateTime.of(date).dayOfYear();
}

Source code * * datetime of(date). Dayofyear() * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, getField The method is introduced above. There is no water here.

/**
 * Get the specified date, which is the day of the year in which the date is located, starting from 1
 *
 * @return Day, 1 means the first day
 * @since 5.3.6
 */
public int dayOfYear() {
   return getField(DateField.DAY_OF_YEAR);
}

Method name: dateutil dayOfWeek(java.util.Date)

Method description

Source code analysis I

/**
 * Get the day of the week for the specified date, 1 for Sunday and 2 for Monday
 *
 * @param date date
 * @return day
 */
public static int dayOfWeek(Date date) {
   return DateTime.of(date).dayOfWeek();
}

Source code * * datetime of(date). Dayofweek() * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, getField The method is introduced above. There is no water here.

/**
 * Get the day of the week for the specified date, 1 for Sunday and 2 for Monday
 *
 * @return What day is today?
 */
public int dayOfWeek() {
   return getField(DateField.DAY_OF_WEEK);
}

Method name: dateutil dayOfWeekEnum(java.util.Date)

Method description

Source code analysis I

/**
 * Gets the day of the week the specified date is
 *
 * @param date date
 * @return {@link Week}
 */
public static Week dayOfWeekEnum(Date date) {
   return DateTime.of(date).dayOfWeekEnum();
}

Source code * * datetime of(date). Dayofweekenum() * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, dayOfWeek() Is to get the day of the week for the specified date. 1 means Sunday and 2 means Monday

**Week.of(int) * * is to convert the corresponding value into the corresponding enumeration week

/**
 * Gets the day of the week the specified date is
 *
 * @return {@link Week}
 */
public Week dayOfWeekEnum() {
   return Week.of(dayOfWeek());
}

Method name: dateutil hour(java.util.Date, boolean)

Method description

Source code analysis I

/**
 * Get the hours section of the specified date < br >
 *
 * @param date          date
 * @param is24HourClock 24-hour system
 * @return Hours
 */
public static int hour(Date date, boolean is24HourClock) {
   return DateTime.of(date).hour(is24HourClock);
}

Source code * * datetime of(date). Hour (is24hourclock) * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, getField The method is introduced above. There is no water here.

is24HourClock ? DateField.HOUR_OF_DAY : DateField.HOUR: is24hour clock

DateField.HOUR_OF_DAY: hour, for 24-hour system

DateField.HOUR: hour, for 12 hour system

/**
 * Get the hours section of the specified date < br >
 *
 * @param is24HourClock 24-hour system
 * @return Hours
 */
public int hour(boolean is24HourClock) {
   return getField(is24HourClock ? DateField.HOUR_OF_DAY : DateField.HOUR);
}

Method name: dateutil minute(java.util.Date)

Method description

Source code analysis I

/**
 * Get the minutes section of the specified date < br >
 * For example: 10:04:15.250 = 4
 *
 * @param date date
 * @return Minutes
 */
public static int minute(Date date) {
   return DateTime.of(date).minute();
}

Source code * * datetime of(date). Minute() * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, getField The method is introduced above. There is no water here.

/**
 * Get the minutes section of the specified date < br >
 * For example: 10:04:15.250 = 4
 *
 * @return Minutes
 */
public int minute() {
   return getField(DateField.MINUTE);
}

Method name: dateutil second(java.util.Date)

Method description

Source code analysis I

/**
 * Gets the seconds portion of the specified date < br >
 *
 * @param date date
 * @return Seconds
 */
public static int second(Date date) {
   return DateTime.of(date).second();
}

Source code * * datetime of(date). Second() * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, getField The method is introduced above. There is no water here.

/**
 * Gets the seconds portion of the specified date < br >
 *
 * @return Seconds
 */
public int second() {
   return getField(DateField.SECOND);
}

Method name: dateutil millisecond(java.util.Date)

Method description

Source code analysis I

	/**
	 * Gets the milliseconds portion of the specified date < br >
	 *
	 * @param date date
	 * @return Msec 
	 */
	public static int millisecond(Date date) {
		return DateTime.of(date).millisecond();
	}

Source code * * datetime of(date). Millisecond() * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, getField The method is introduced above. There is no water here.

/**
 * Gets the milliseconds portion of the specified date < br >
 *
 * @return Msec 
 */
public int millisecond() {
   return getField(DateField.MILLISECOND);
}

Method name: dateutil isAM(java.util.Date)

Method description

Is it morning

Source code analysis I

/**
 * Is it morning
 *
 * @param date date
 * @return Is it morning
 */
public static boolean isAM(Date date) {
   return DateTime.of(date).isAM();
}

Source code * * datetime of(date). ISAM () * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, getField The method is introduced above. There is no water here.

Made a judgment calendar AM == getField(DateField.AM_PM)

/**
 * Is it morning
 *
 * @return Is it morning
 */
public boolean isAM() {
   return Calendar.AM == getField(DateField.AM_PM);
}

Method name: dateutil isPM(java.util.Date)

Method description

Is it afternoon

Source code analysis I

/**
 * Is it afternoon
 *
 * @param date date
 * @return Is it afternoon
 */
public static boolean isPM(Date date) {
   return DateTime.of(date).isPM();
}

Source code * * datetime of(date). ISPM () * * can be disassembled into two parts

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

DateTime.of(date) There is an introduction above. There is no water here.

Part two, getField The method is introduced above. There is no water here.

Made a judgment calendar PM== getField(DateField.AM_PM)

/**
 * Is it afternoon
 *
 * @return Is it afternoon
 */
public boolean isPM() {
   return Calendar.PM == getField(DateField.AM_PM);
}

Method name: dateutil thisYear()

Method description

Return this year

Source code analysis I

/**
 * @return this year
 */
public static int thisYear() {
   return year(date());
}

The * * year(date()) * * of the source code can be disassembled into two parts

  • DateTime.date() gets the current time and returns the datetime type
  • DateTime.year() gets the part of the year Source code analysis

DateTime.date()

/**
 * Current time, converted to {@ link DateTime} object
 *
 * @return current time 
 */
public static DateTime date() {
   return new DateTime();
}

Method name: dateutil thisMonth()

Method description

Returns the current month

Source code analysis I

/**
 * @return Current month
 */
public static int thisMonth() {
   return month(date());
}

The * * month(date()) * * of the source code can be disassembled into two parts

  • DateTime.date() gets the current time and returns the datetime type
  • DateTime.month() gets the part of the year Source code analysis

DateTime.date()

/**
 * Current time, converted to {@ link DateTime} object
 *
 * @return current time 
 */
public static DateTime date() {
   return new DateTime();
}

Method name: dateutil thisMonthEnum()

Method description

Returns the current month

Source code analysis I

/**
 * @return Current month {@ link Month}
 */
public static Month thisMonthEnum() {
   return monthEnum(date());
}

The * * monthEnum(date()) * * of the source code can be disassembled into two parts

  • DateTime.date() gets the current time and returns the datetime type
  • DateTime.monthEnum() gets the year's portion Source code analysis

DateTime.date()

/**
 * Current time, converted to {@ link DateTime} object
 *
 * @return current time 
 */
public static DateTime date() {
   return new DateTime();
}

Method name: dateutil thisWeekOfYear()

Method description

Returns the week ordinal of the year of the current date

Source code analysis I

/**
 * @return The week ordinal of the year in which the current date is located
 */
public static int thisWeekOfYear() {
   return weekOfYear(date());
}

The * * weekOfYear(date()) * * of the source code can be disassembled into two parts

  • DateTime.date() gets the current time and returns the datetime type
  • DateTime.weekOfYear() gets the part of the year Source code analysis

DateTime.date()

/**
 * Current time, converted to {@ link DateTime} object
 *
 * @return current time 
 */
public static DateTime date() {
   return new DateTime();
}

Method name: dateutil thisWeekOfMonth()

Method description

Returns the week ordinal of the month in which the current date is located

Source code analysis I

	/**
	 * @return The week ordinal of the month in which the current date is located
	 */
	public static int thisWeekOfMonth() {
		return weekOfMonth(date());
	}

The * * weekOfMonth(date()) * * of the source code can be disassembled into two parts

  • DateTime.date() gets the current time and returns the datetime type
  • DateTime.weekOfMonth() gets the part of the year Source code analysis

DateTime.date()

/**
 * Current time, converted to {@ link DateTime} object
 *
 * @return current time 
 */
public static DateTime date() {
   return new DateTime();
}

Method name: dateutil thisDayOfMonth()

Method description

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

Source code analysis I

	/**
	 * @return The current date is the day of the month in which this date is located
	 */
	public static int thisDayOfMonth() {
		return dayOfMonth(date());
	}

The * * dayOfMonth(date()) * * of the source code can be disassembled into two parts

  • DateTime.date() gets the current time and returns the datetime type
  • DateTime.dayOfMonth() gets the part of the year Source code analysis

DateTime.date()

/**
 * Current time, converted to {@ link DateTime} object
 *
 * @return current time 
 */
public static DateTime date() {
   return new DateTime();
}

Method name: dateutil thisDayOfWeek()

Method description

Returns the day of the week the current date is

Source code analysis I

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

The * * dayOfWeek(date()) * * of the source code can be disassembled into two parts

  • DateTime.date() gets the current time and returns the datetime type
  • DateTime.dayOfWeek() gets the part of the year Source code analysis

DateTime.date()

/**
 * Current time, converted to {@ link DateTime} object
 *
 * @return current time 
 */
public static DateTime date() {
   return new DateTime();
}

Method name: dateutil thisDayOfWeekEnum()

Method description

Returns the day of the week the current date is

Source code analysis I

	/**
	 * @return The current date is the day of the week {@ link Week}
	 */
	public static Week thisDayOfWeekEnum() {
		return dayOfWeekEnum(date());
	}

The * * dayOfWeekEnum(date()) * * of the source code can be disassembled into two parts

  • DateTime.date() gets the current time and returns the datetime type
  • DateTime.dayOfWeekEnum() gets the part of the year Source code analysis

DateTime.date()

/**
 * Current time, converted to {@ link DateTime} object
 *
 * @return current time 
 */
public static DateTime date() {
   return new DateTime();
}

Method name: dateutil thisHour(boolean)

Method description

Returns the hours portion of the current date

Source code analysis I

	/**
	 * @param is24HourClock 24-hour system
	 * @return The hours portion of the current date < br >
	 */
	public static int thisHour(boolean is24HourClock) {
		return hour(date(), is24HourClock);
	}

The * * hour(date(), is24HourClock) * * of the source code can be disassembled into two parts

  • DateTime.date() gets the current time and returns the datetime type
  • DateTime.hour() gets the part of the year Source code analysis

DateTime.date()

/**
 * Current time, converted to {@ link DateTime} object
 *
 * @return current time 
 */
public static DateTime date() {
   return new DateTime();
}

Method name: dateutil thisMinute()

Method description

Returns the minutes portion of the current date

Source code analysis I

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

The * * minute(date()) * * of the source code can be disassembled into two parts

  • DateTime.date() gets the current time and returns the datetime type
  • DateTime.minute() gets the part of the year Source code analysis

DateTime.date()

/**
 * Current time, converted to {@ link DateTime} object
 *
 * @return current time 
 */
public static DateTime date() {
   return new DateTime();
}

Method name: dateutil thisSecond()

Method description

Returns the seconds portion of the current date

Source code analysis I

	/**
	 * @return The seconds portion of the current date < br >
	 */
	public static int thisSecond() {
		return second(date());
	}

The * * second(date()) * * of the source code can be disassembled into two parts

  • DateTime.date() gets the current time and returns the datetime type
  • DateTime.second() gets the part of the year Source code analysis

DateTime.date()

/**
 * Current time, converted to {@ link DateTime} object
 *
 * @return current time 
 */
public static DateTime date() {
   return new DateTime();
}

Method name: dateutil thisMillisecond()

Method description

Returns the milliseconds portion of the current date

Source code analysis I

	/**
	 * @return The milliseconds portion of the current date < br >
	 */
	public static int thisMillisecond() {
		return millisecond(date());
	}

The * * millisecond(date()) * * of the source code can be disassembled into two parts

  • DateTime.date() gets the current time and returns the datetime type
  • DateTime.millisecond() gets the part of the year Source code analysis

DateTime.date()

/**
 * Current time, converted to {@ link DateTime} object
 *
 * @return current time 
 */
public static DateTime date() {
   return new DateTime();
}

Method name: dateutil yearAndQuarter(java.util.Date)

Method description

Get the specified date, year and season < br >
Format: [20131] indicates the first quarter of 2013

Source code analysis I

/**
 * Get the specified date, year and season < br >
 * Format: [20131] indicates the first quarter of 2013
 *
 * @param date date
 * @return Quarter ,Similar to 20132
 */
public static String yearAndQuarter(Date date) {
   return yearAndQuarter(calendar(date));
}

The * * yearAndQuarter(calendar(date)) * * of the source code can be disassembled into two parts

  • Calendar (date): convert a date object to a calendar object
  • yearAndQuarter(Calendar)

yearAndQuarter method code analysis:

Get year cal get(Calendar.YEAR)

Get quarterly cal get(Calendar.MONTH) / 3 + 1

Then use StringBuilder to splice strings

/**
 * Get the specified date, year and quarter < br >
 * Format: [20131] indicates the first quarter of 2013
 *
 * @param cal date
 * @return Year and quarter, format 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();
}

Method name: dateutil yearAndQuarter(java.util.Date, java.util.Date)

Method description

Obtain the year and season within the specified date interval < br >

Source code analysis I

/**
 * Obtain the year and season within the specified date interval < br >
 *
 * @param startDate Start date (inclusive)
 * @param endDate   End date (inclusive)
 * @return Quarterly list, element 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());
}

As shown in the above code, there is a blank judgment processing

Then, write a while loop and save the qualified year and quarter in the LinkedHashSet. After saving the string of a year and quarter, the start time will be increased by 3 months. If the start time exceeds the end time, let the end time be the start time, and end the loop after processing

/**
 * Obtain the year and quarter within the specified date range < br >
 *
 * @param startDate Start date (inclusive)
 * @param endDate   End date (inclusive)
 * @return Quarterly list, element similar to 20132
 * @since 4.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, let the end time be the start time, and end the cycle after processing
      quarters.add(yearAndQuarter(cal));

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

   return quarters;
}

Keywords: Java source code analysis hutool

Added by RogerInHawaii on Fri, 14 Jan 2022 00:57:32 +0200