forked from plusone/plusone-commons
补充注释。
parent
c58e799b1e
commit
4ac281d65b
|
@ -7,22 +7,41 @@ import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.util.Numbers;
|
import xyz.zhouxy.plusone.commons.util.Numbers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 季度
|
||||||
|
*
|
||||||
|
* @author zhouxy
|
||||||
|
*/
|
||||||
public enum Quarter {
|
public enum Quarter {
|
||||||
|
/** 第一季度 */
|
||||||
Q1(1, "Q1"),
|
Q1(1, "Q1"),
|
||||||
|
/** 第二季度 */
|
||||||
Q2(2, "Q2"),
|
Q2(2, "Q2"),
|
||||||
|
/** 第三季度 */
|
||||||
Q3(3, "Q3"),
|
Q3(3, "Q3"),
|
||||||
|
/** 第四季度 */
|
||||||
Q4(4, "Q4"),
|
Q4(4, "Q4"),
|
||||||
;
|
;
|
||||||
|
|
||||||
|
/** 季度值 (1/2/3/4) */
|
||||||
private final int value;
|
private final int value;
|
||||||
|
/** 季度名称 */
|
||||||
private final String displayName;
|
private final String displayName;
|
||||||
|
|
||||||
|
/** 季度开始月份 */
|
||||||
private final int startMonthValue;
|
private final int startMonthValue;
|
||||||
|
/** 季度开始日期 */
|
||||||
private final MonthDay startMonthDay;
|
private final MonthDay startMonthDay;
|
||||||
|
|
||||||
|
/** 季度结束月份 */
|
||||||
private final int lastMonthValue;
|
private final int lastMonthValue;
|
||||||
|
/** 季度结束日期 */
|
||||||
private final MonthDay lastMonthDay;
|
private final MonthDay lastMonthDay;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value 季度值 (1/2/3/4)
|
||||||
|
* @param str 季度名称
|
||||||
|
*/
|
||||||
Quarter(int value, String str) {
|
Quarter(int value, String str) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.displayName = str;
|
this.displayName = str;
|
||||||
|
@ -36,20 +55,47 @@ public enum Quarter {
|
||||||
this.lastMonthDay = MonthDay.of(lastMonth, (value == 1 || value == 4) ? 31 : 30);
|
this.lastMonthDay = MonthDay.of(lastMonth, (value == 1 || value == 4) ? 31 : 30);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据给定的月份值返回对应的季度
|
||||||
|
*
|
||||||
|
* @param monthValue 月份值,取值范围为1到12
|
||||||
|
* @return 对应的季度
|
||||||
|
* @throws IllegalArgumentException 如果月份值不在有效范围内(1到12),将抛出异常
|
||||||
|
*/
|
||||||
public static Quarter fromMonth(int monthValue) {
|
public static Quarter fromMonth(int monthValue) {
|
||||||
Preconditions.checkArgument(Numbers.between(monthValue, 1, 13), "Invalid value for MonthOfYear: " + monthValue);
|
Preconditions.checkArgument(Numbers.between(monthValue, 1, 13), "Invalid value for MonthOfYear: " + monthValue);
|
||||||
return of(computeQuarterValueInternal(monthValue));
|
return of(computeQuarterValueInternal(monthValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据给定的月份返回对应的季度
|
||||||
|
*
|
||||||
|
* @param month 月份
|
||||||
|
* @return 对应的季度
|
||||||
|
*/
|
||||||
public static Quarter fromMonth(Month month) {
|
public static Quarter fromMonth(Month month) {
|
||||||
final int monthValue = month.getValue();
|
final int monthValue = month.getValue();
|
||||||
return of(computeQuarterValueInternal(monthValue));
|
return of(computeQuarterValueInternal(monthValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据指定的年份,获取一个新的 YearQuarter 实例
|
||||||
|
* 此方法允许在保持当前季度信息不变的情况下,更改年份
|
||||||
|
*
|
||||||
|
* @param year 指定的年份
|
||||||
|
* @return 返回一个新的 YearQuarter 实例,年份更新为指定的年份
|
||||||
|
*/
|
||||||
public final YearQuarter atYear(int year) {
|
public final YearQuarter atYear(int year) {
|
||||||
return YearQuarter.of(year, this);
|
return YearQuarter.of(year, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据给定的季度值返回对应的季度
|
||||||
|
*
|
||||||
|
* @param value 季度值 (1/2/3/4)
|
||||||
|
* @return 对应的季度
|
||||||
|
* @throws IllegalArgumentException 如果季度值不在有效范围内(1到4),将抛出异常
|
||||||
|
*/
|
||||||
public static Quarter of(int value) {
|
public static Quarter of(int value) {
|
||||||
switch (value) {
|
switch (value) {
|
||||||
case 1:
|
case 1:
|
||||||
|
@ -65,6 +111,13 @@ public enum Quarter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据给定的季度名称返回对应的季度
|
||||||
|
*
|
||||||
|
* @param str 季度名称
|
||||||
|
* @return 对应的季度
|
||||||
|
* @throws IllegalArgumentException 如果季度名称不在有效范围内(Q1/Q2/Q3/Q4),将抛出异常
|
||||||
|
*/
|
||||||
public static Quarter of(String str) {
|
public static Quarter of(String str) {
|
||||||
switch (str) {
|
switch (str) {
|
||||||
case "Q1":
|
case "Q1":
|
||||||
|
@ -116,11 +169,13 @@ public enum Quarter {
|
||||||
|
|
||||||
// Getters end
|
// Getters end
|
||||||
|
|
||||||
|
// Internal
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算季度
|
* 计算给定月份对应的季度值
|
||||||
*
|
*
|
||||||
* @param monthValue 1~12
|
* @param monthValue 月份值,取值范围为1到12
|
||||||
* @return 季度。1~4
|
* @return 对应的季度值
|
||||||
*/
|
*/
|
||||||
private static int computeQuarterValueInternal(int monthValue) {
|
private static int computeQuarterValueInternal(int monthValue) {
|
||||||
return (monthValue - 1) / 3 + 1;
|
return (monthValue - 1) / 3 + 1;
|
||||||
|
|
|
@ -5,16 +5,29 @@ import java.time.Month;
|
||||||
import java.time.YearMonth;
|
import java.time.YearMonth;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表示年份与季度
|
||||||
|
*
|
||||||
|
* @author zhouxy
|
||||||
|
*/
|
||||||
public final class YearQuarter {
|
public final class YearQuarter {
|
||||||
|
|
||||||
|
/** 年份 */
|
||||||
private final int year;
|
private final int year;
|
||||||
|
/** 季度 */
|
||||||
private final Quarter quarter;
|
private final Quarter quarter;
|
||||||
|
/** 季度开始日期 */
|
||||||
private final LocalDate startDate;
|
private final LocalDate startDate;
|
||||||
|
/** 季度结束日期 */
|
||||||
private final LocalDate lastDate;
|
private final LocalDate lastDate;
|
||||||
|
|
||||||
private YearQuarter(int year, Quarter quarter) {
|
private YearQuarter(int year, @Nonnull Quarter quarter) {
|
||||||
Preconditions.checkNotNull(quarter, "Quarter can not be null.");
|
Preconditions.checkNotNull(quarter, "Quarter can not be null.");
|
||||||
this.year = year;
|
this.year = year;
|
||||||
this.quarter = quarter;
|
this.quarter = quarter;
|
||||||
|
@ -22,15 +35,34 @@ public final class YearQuarter {
|
||||||
this.lastDate = quarter.getLastMonthDay().atYear(year);
|
this.lastDate = quarter.getLastMonthDay().atYear(year);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static YearQuarter of(int year, Quarter quarter) {
|
/**
|
||||||
|
* 根据指定年份与季度,创建 {@link YearQuarter} 实例
|
||||||
|
*
|
||||||
|
* @param year 年份
|
||||||
|
* @param quarter 季度
|
||||||
|
* @return {@link YearQuarter} 实例
|
||||||
|
*/
|
||||||
|
public static YearQuarter of(int year, @Nonnull Quarter quarter) {
|
||||||
return new YearQuarter(year, quarter);
|
return new YearQuarter(year, quarter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static YearQuarter of(LocalDate date) {
|
/**
|
||||||
|
* 根据指定日期,判断日期所在的年份与季度,创建 {@link YearQuarter} 实例
|
||||||
|
*
|
||||||
|
* @param date 日期
|
||||||
|
* @return {@link YearQuarter} 实例
|
||||||
|
*/
|
||||||
|
public static YearQuarter of(@Nonnull LocalDate date) {
|
||||||
return new YearQuarter(date.getYear(), Quarter.fromMonth(date.getMonth()));
|
return new YearQuarter(date.getYear(), Quarter.fromMonth(date.getMonth()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static YearQuarter of(Date date) {
|
/**
|
||||||
|
* 根据指定日期,判断日期所在的年份与季度,创建 {@link YearQuarter} 实例
|
||||||
|
*
|
||||||
|
* @param date 日期
|
||||||
|
* @return {@link YearQuarter} 实例
|
||||||
|
*/
|
||||||
|
public static YearQuarter of(@Nonnull Date date) {
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
final int year = date.getYear() + 1900;
|
final int year = date.getYear() + 1900;
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
|
@ -38,14 +70,28 @@ public final class YearQuarter {
|
||||||
return of(year, Quarter.fromMonth(month));
|
return of(year, Quarter.fromMonth(month));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据指定日期,判断日期所在的年份与季度,创建 {@link YearQuarter} 实例
|
||||||
|
*
|
||||||
|
* @param date 日期
|
||||||
|
* @return {@link YearQuarter} 实例
|
||||||
|
*/
|
||||||
public static YearQuarter of(Calendar date) {
|
public static YearQuarter of(Calendar date) {
|
||||||
return of(date.get(Calendar.YEAR), Quarter.fromMonth(date.get(Calendar.MONTH) + 1));
|
return of(date.get(Calendar.YEAR), Quarter.fromMonth(date.get(Calendar.MONTH) + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据指定年月,判断其所在的年份与季度,创建 {@link YearQuarter} 实例
|
||||||
|
*
|
||||||
|
* @param yearMonth 年月
|
||||||
|
* @return {@link YearQuarter} 实例
|
||||||
|
*/
|
||||||
public static YearQuarter of(YearMonth yearMonth) {
|
public static YearQuarter of(YearMonth yearMonth) {
|
||||||
return of(yearMonth.getYear(), Quarter.fromMonth(yearMonth.getMonth()));
|
return of(yearMonth.getYear(), Quarter.fromMonth(yearMonth.getMonth()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
|
||||||
public int getYear() {
|
public int getYear() {
|
||||||
return year;
|
return year;
|
||||||
}
|
}
|
||||||
|
@ -77,4 +123,37 @@ public final class YearQuarter {
|
||||||
public LocalDate getLastDate() {
|
public LocalDate getLastDate() {
|
||||||
return lastDate;
|
return lastDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Getters end
|
||||||
|
|
||||||
|
// hashCode & equals
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(year, quarter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
YearQuarter other = (YearQuarter) obj;
|
||||||
|
return year == other.year && quarter == other.quarter;
|
||||||
|
}
|
||||||
|
|
||||||
|
// toString
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回 {@link YearQuarter} 的字符串表示形式,如 "Q3 2024"
|
||||||
|
*
|
||||||
|
* @return {@link YearQuarter} 的字符串表示形式
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.quarter.getDisplayName() + " " + this.year;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.LocalTime;
|
import java.time.LocalTime;
|
||||||
import java.time.Month;
|
import java.time.Month;
|
||||||
|
import java.time.YearMonth;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
@ -21,93 +22,205 @@ import xyz.zhouxy.plusone.commons.base.Quarter;
|
||||||
import xyz.zhouxy.plusone.commons.base.YearQuarter;
|
import xyz.zhouxy.plusone.commons.base.YearQuarter;
|
||||||
import xyz.zhouxy.plusone.commons.collection.MapWrapper;
|
import xyz.zhouxy.plusone.commons.collection.MapWrapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日期时间工具类
|
||||||
|
*
|
||||||
|
* @author zhouxy
|
||||||
|
*/
|
||||||
public class DateTimeTools {
|
public class DateTimeTools {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存时间格式化器
|
||||||
|
*/
|
||||||
private static final MapWrapper<String, DateTimeFormatter> DATE_TIME_FORMATTER_CACHE = MapWrapper
|
private static final MapWrapper<String, DateTimeFormatter> DATE_TIME_FORMATTER_CACHE = MapWrapper
|
||||||
.<String, DateTimeFormatter>wrap(new SafeConcurrentHashMap<>())
|
.<String, DateTimeFormatter>wrap(new SafeConcurrentHashMap<>())
|
||||||
.keyChecker(pattern -> Preconditions.checkArgument(StringUtils.isNotBlank(pattern), "The pattern could not be blank."))
|
.keyChecker(pattern -> Preconditions.checkArgument(StringUtils.isNotBlank(pattern), "The pattern could not be blank."))
|
||||||
.valueChecker(formatter -> Preconditions.checkNotNull(formatter, "The formatter could not be null."))
|
.valueChecker(formatter -> Preconditions.checkNotNull(formatter, "The formatter could not be null."))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取时间格式化器
|
||||||
|
*
|
||||||
|
* @param pattern 时间格式
|
||||||
|
* @return 时间格式化器
|
||||||
|
*/
|
||||||
public static DateTimeFormatter getDateTimeFormatter(String pattern) {
|
public static DateTimeFormatter getDateTimeFormatter(String pattern) {
|
||||||
return DATE_TIME_FORMATTER_CACHE.computeIfAbsent(pattern, DateTimeFormatter::ofPattern);
|
return DATE_TIME_FORMATTER_CACHE.computeIfAbsent(pattern, DateTimeFormatter::ofPattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将日期时间转换为指定格式的字符串
|
||||||
|
*
|
||||||
|
* @param pattern 时间格式
|
||||||
|
* @param dateTime 日期时间
|
||||||
|
* @return 格式化的字符串
|
||||||
|
*/
|
||||||
public static String toString(String pattern, ZonedDateTime dateTime) {
|
public static String toString(String pattern, ZonedDateTime dateTime) {
|
||||||
return getDateTimeFormatter(pattern).format(dateTime);
|
return getDateTimeFormatter(pattern).format(dateTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将时间戳转换为指定格式的字符串,使用系统默认时区
|
||||||
|
*
|
||||||
|
* @param pattern 时间格式
|
||||||
|
* @param instant 时间戳
|
||||||
|
* @return 格式化的字符串
|
||||||
|
*/
|
||||||
public static String toString(String pattern, Instant instant) {
|
public static String toString(String pattern, Instant instant) {
|
||||||
ZonedDateTime dateTime = instant.atZone(ZoneId.systemDefault());
|
ZonedDateTime dateTime = instant.atZone(ZoneId.systemDefault());
|
||||||
return toString(pattern, dateTime);
|
return toString(pattern, dateTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将时间戳转换为指定格式的字符串,使用指定时区
|
||||||
|
*
|
||||||
|
* @param pattern 时间格式
|
||||||
|
* @param instant 时间戳
|
||||||
|
* @param zone 时区
|
||||||
|
* @return 格式化的字符串
|
||||||
|
*/
|
||||||
public static String toString(String pattern, Instant instant, ZoneId zone) {
|
public static String toString(String pattern, Instant instant, ZoneId zone) {
|
||||||
ZonedDateTime dateTime = instant.atZone(zone);
|
ZonedDateTime dateTime = instant.atZone(zone);
|
||||||
return toString(pattern, dateTime);
|
return toString(pattern, dateTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 指定格式,返回当前时间戳对应的字符串
|
||||||
|
*
|
||||||
|
* @param pattern 时间格式
|
||||||
|
* @return 格式化的字符串
|
||||||
|
*/
|
||||||
public static String nowStr(String pattern) {
|
public static String nowStr(String pattern) {
|
||||||
return toString(pattern, ZonedDateTime.now());
|
return toString(pattern, ZonedDateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 指定格式,返回当前时间戳对应的字符串,使用指定时区
|
||||||
|
*
|
||||||
|
* @param pattern 时间格式
|
||||||
|
* @param zone 时区
|
||||||
|
* @return 格式化的字符串
|
||||||
|
*/
|
||||||
public static String nowStr(String pattern, ZoneId zone) {
|
public static String nowStr(String pattern, ZoneId zone) {
|
||||||
return toString(pattern, Instant.now().atZone(zone));
|
return toString(pattern, Instant.now().atZone(zone));
|
||||||
}
|
}
|
||||||
|
|
||||||
// toDate
|
// toDate
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将时间戳转换为 {@link Date} 对象
|
||||||
|
*
|
||||||
|
* @param timeMillis 时间戳
|
||||||
|
* @return {@link Date} 对象
|
||||||
|
*/
|
||||||
public static Date toDate(long timeMillis) {
|
public static Date toDate(long timeMillis) {
|
||||||
return Date.from(Instant.ofEpochMilli(timeMillis));
|
return Date.from(Instant.ofEpochMilli(timeMillis));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 {@link Calendar} 对象转换为 {@link Date} 对象
|
||||||
|
*
|
||||||
|
* @param calendar {@link Calendar} 对象
|
||||||
|
* @return {@link Date} 对象
|
||||||
|
*/
|
||||||
public static Date toDate(Calendar calendar) {
|
public static Date toDate(Calendar calendar) {
|
||||||
return calendar.getTime();
|
return calendar.getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 {@link Instant} 对象转换为 {@link Date} 对象
|
||||||
|
*
|
||||||
|
* @param instant {@link Instant} 对象
|
||||||
|
* @return {@link Date} 对象
|
||||||
|
*/
|
||||||
public static Date toDate(Instant instant) {
|
public static Date toDate(Instant instant) {
|
||||||
return Date.from(instant);
|
return Date.from(instant);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 {@link ZonedDateTime} 对象转换为 {@link Date} 对象
|
||||||
|
*
|
||||||
|
* @param zonedDateTime {@link ZonedDateTime} 对象
|
||||||
|
* @return {@link Date} 对象
|
||||||
|
*/
|
||||||
public static Date toDate(ZonedDateTime zonedDateTime) {
|
public static Date toDate(ZonedDateTime zonedDateTime) {
|
||||||
return Date.from(zonedDateTime.toInstant());
|
return Date.from(zonedDateTime.toInstant());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用指定时区,将 {@link LocalDateTime} 对象转换为 {@link Date} 对象
|
||||||
|
*
|
||||||
|
* @param localDateTime {@link LocalDateTime} 对象
|
||||||
|
* @param zone 时区
|
||||||
|
* @return {@link Date} 对象
|
||||||
|
*/
|
||||||
public static Date toDate(LocalDateTime localDateTime, ZoneId zone) {
|
public static Date toDate(LocalDateTime localDateTime, ZoneId zone) {
|
||||||
return Date.from(ZonedDateTime.of(localDateTime, zone).toInstant());
|
return Date.from(ZonedDateTime.of(localDateTime, zone).toInstant());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用指定时区,将 {@link LocalDate} 和 {@link LocalTime} 对象转换为 {@link Date} 对象
|
||||||
|
*
|
||||||
|
* @param localDate {@link LocalDate} 对象
|
||||||
|
* @param localTime {@link LocalTime} 对象
|
||||||
|
* @param zone 时区
|
||||||
|
* @return {@link Date} 对象
|
||||||
|
*/
|
||||||
public static Date toDate(LocalDate localDate, LocalTime localTime, ZoneId zone) {
|
public static Date toDate(LocalDate localDate, LocalTime localTime, ZoneId zone) {
|
||||||
return Date.from(ZonedDateTime.of(localDate, localTime, zone).toInstant());
|
return Date.from(ZonedDateTime.of(localDate, localTime, zone).toInstant());
|
||||||
}
|
}
|
||||||
|
|
||||||
// toInstant
|
// toInstant
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将时间戳转换为 {@link Instant} 对象
|
||||||
|
*
|
||||||
|
* @param timeMillis 时间戳
|
||||||
|
* @return {@link Instant} 对象
|
||||||
|
*/
|
||||||
public static Instant toInstant(long timeMillis) {
|
public static Instant toInstant(long timeMillis) {
|
||||||
return Instant.ofEpochMilli(timeMillis);
|
return Instant.ofEpochMilli(timeMillis);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 {@link Date} 对象转换为 {@link Instant} 对象
|
||||||
|
*
|
||||||
|
* @param date {@link Date} 对象
|
||||||
|
* @return {@link Instant} 对象
|
||||||
|
*/
|
||||||
public static Instant toInstant(Date date) {
|
public static Instant toInstant(Date date) {
|
||||||
return date.toInstant();
|
return date.toInstant();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 {@link Calendar} 对象转换为 {@link Instant} 对象
|
||||||
|
*
|
||||||
|
* @param calendar {@link Calendar} 对象
|
||||||
|
* @return {@link Instant} 对象
|
||||||
|
*/
|
||||||
public static Instant toInstant(Calendar calendar) {
|
public static Instant toInstant(Calendar calendar) {
|
||||||
return calendar.toInstant();
|
return calendar.toInstant();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 {@link Instant} 对象
|
* 将 {@link ZonedDateTime} 对象转换为 {@link Instant} 对象
|
||||||
*
|
*
|
||||||
* @param zonedDateTime 带时区信息的地区时间
|
* @param zonedDateTime {@link ZonedDateTime} 对象
|
||||||
* @return 时间点
|
* @return {@link Instant} 对象
|
||||||
*
|
* @deprecated 请使用 {@link ZonedDateTime#toInstant()} 方法
|
||||||
* @deprecated 使用 {@link ZonedDateTime#toInstant()}
|
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static Instant toInstant(ZonedDateTime zonedDateTime) { // NOSONAR
|
public static Instant toInstant(ZonedDateTime zonedDateTime) { // NOSONAR
|
||||||
return zonedDateTime.toInstant();
|
return zonedDateTime.toInstant();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用指定时区,将 {@link LocalDateTime} 对象转换为 {@link Instant} 对象
|
||||||
|
*
|
||||||
|
* @param LocalDateTime {@link LocalDateTime} 对象
|
||||||
|
* @param zone 时区
|
||||||
|
* @return {@link Instant} 对象
|
||||||
|
*/
|
||||||
public static Instant toInstant(LocalDateTime localDateTime, ZoneId zone) {
|
public static Instant toInstant(LocalDateTime localDateTime, ZoneId zone) {
|
||||||
return ZonedDateTime.of(localDateTime, zone).toInstant();
|
return ZonedDateTime.of(localDateTime, zone).toInstant();
|
||||||
}
|
}
|
||||||
|
@ -159,14 +272,35 @@ public class DateTimeTools {
|
||||||
return ZonedDateTime.ofInstant(dateTime.toInstant(), timeZone.toZoneId());
|
return ZonedDateTime.ofInstant(dateTime.toInstant(), timeZone.toZoneId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用 {@code calendar} 对象的时区信息,将 {@link Calendar} 对象转换为 {@link ZonedDateTime}
|
||||||
|
* 对象。
|
||||||
|
*
|
||||||
|
* @param calendar{@link Calendar} 对象
|
||||||
|
* @return {@link ZonedDateTime} 对象
|
||||||
|
*/
|
||||||
public static ZonedDateTime toZonedDateTime(Calendar calendar) {
|
public static ZonedDateTime toZonedDateTime(Calendar calendar) {
|
||||||
return calendar.toInstant().atZone(calendar.getTimeZone().toZoneId());
|
return calendar.toInstant().atZone(calendar.getTimeZone().toZoneId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用指定的时区,将 {@link Calendar} 对象转换为 {@link ZonedDateTime} 对象。
|
||||||
|
*
|
||||||
|
* @param calendar {@link Calendar} 对象
|
||||||
|
* @param zone 时区
|
||||||
|
* @return {@link ZonedDateTime} 对象
|
||||||
|
*/
|
||||||
public static ZonedDateTime toZonedDateTime(Calendar calendar, ZoneId zone) {
|
public static ZonedDateTime toZonedDateTime(Calendar calendar, ZoneId zone) {
|
||||||
return calendar.toInstant().atZone(zone);
|
return calendar.toInstant().atZone(zone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用指定的时区,将 {@link Calendar} 对象转换为 {@link ZonedDateTime} 对象。
|
||||||
|
*
|
||||||
|
* @param calendar {@link Calendar} 对象
|
||||||
|
* @param zone 时区
|
||||||
|
* @return {@link ZonedDateTime} 对象
|
||||||
|
*/
|
||||||
public static ZonedDateTime toZonedDateTime(Calendar calendar, TimeZone zone) {
|
public static ZonedDateTime toZonedDateTime(Calendar calendar, TimeZone zone) {
|
||||||
return calendar.toInstant().atZone(zone.toZoneId());
|
return calendar.toInstant().atZone(zone.toZoneId());
|
||||||
}
|
}
|
||||||
|
@ -220,14 +354,35 @@ public class DateTimeTools {
|
||||||
return LocalDateTime.ofInstant(dateTime.toInstant(), timeZone.toZoneId());
|
return LocalDateTime.ofInstant(dateTime.toInstant(), timeZone.toZoneId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 {@link Calendar} 所表示的时间戳,在指定时区的地区时间。
|
||||||
|
*
|
||||||
|
* @param calendar {@link Calendar} 对象
|
||||||
|
* @param zone 时区
|
||||||
|
* @return 地区时间
|
||||||
|
*/
|
||||||
public static LocalDateTime toLocalDateTime(Calendar calendar, ZoneId zone) {
|
public static LocalDateTime toLocalDateTime(Calendar calendar, ZoneId zone) {
|
||||||
return LocalDateTime.ofInstant(calendar.toInstant(), zone);
|
return LocalDateTime.ofInstant(calendar.toInstant(), zone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 {@link Calendar} 所表示的时间戳,在指定时区的地区时间。
|
||||||
|
*
|
||||||
|
* @param calendar {@link Calendar} 对象
|
||||||
|
* @param zone 时区
|
||||||
|
* @return 地区时间
|
||||||
|
*/
|
||||||
public static LocalDateTime toLocalDateTime(Calendar calendar, TimeZone zone) {
|
public static LocalDateTime toLocalDateTime(Calendar calendar, TimeZone zone) {
|
||||||
return LocalDateTime.ofInstant(calendar.toInstant(), zone.toZoneId());
|
return LocalDateTime.ofInstant(calendar.toInstant(), zone.toZoneId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 {@link ZonedDateTime} 所表示的时间戳,在指定时区的地区时间。
|
||||||
|
*
|
||||||
|
* @param zonedDateTime {@link ZonedDateTime} 对象
|
||||||
|
* @param zone 时区
|
||||||
|
* @return 地区时间
|
||||||
|
*/
|
||||||
public static LocalDateTime toLocalDateTime(ZonedDateTime zonedDateTime, ZoneId zone) {
|
public static LocalDateTime toLocalDateTime(ZonedDateTime zonedDateTime, ZoneId zone) {
|
||||||
return LocalDateTime.ofInstant(zonedDateTime.toInstant(), zone);
|
return LocalDateTime.ofInstant(zonedDateTime.toInstant(), zone);
|
||||||
}
|
}
|
||||||
|
@ -236,28 +391,69 @@ public class DateTimeTools {
|
||||||
|
|
||||||
// toJodaInstant
|
// toJodaInstant
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 {@link java.time.Instant} 转换为 {@link org.joda.time.Instant}
|
||||||
|
*
|
||||||
|
* @param instant {@link java.time.Instant} 对象
|
||||||
|
* @return {@link org.joda.time.Instant} 对象
|
||||||
|
*/
|
||||||
public static org.joda.time.Instant toJodaInstant(java.time.Instant instant) {
|
public static org.joda.time.Instant toJodaInstant(java.time.Instant instant) {
|
||||||
return new org.joda.time.Instant(instant.toEpochMilli());
|
return new org.joda.time.Instant(instant.toEpochMilli());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 {@link java.time.ZonedDateTime} 转换为 {@link org.joda.time.Instant}
|
||||||
|
*
|
||||||
|
* @param zonedDateTime {@link java.time.ZonedDateTime} 对象
|
||||||
|
* @return {@link org.joda.time.Instant} 对象
|
||||||
|
*/
|
||||||
public static org.joda.time.Instant toJodaInstant(java.time.ZonedDateTime zonedDateTime) {
|
public static org.joda.time.Instant toJodaInstant(java.time.ZonedDateTime zonedDateTime) {
|
||||||
return toJodaInstant(zonedDateTime.toInstant());
|
return toJodaInstant(zonedDateTime.toInstant());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算指定时区的地区时间,对应的时间戳。结果为 {@link org.joda.time.Instant} 对象
|
||||||
|
*
|
||||||
|
* @param localDateTime {@link java.time.LocalDateTime} 对象
|
||||||
|
* @param zone 时区
|
||||||
|
* @return {@link org.joda.time.Instant} 对象
|
||||||
|
*/
|
||||||
public static org.joda.time.Instant toJodaInstant(java.time.LocalDateTime localDateTime, java.time.ZoneId zone) {
|
public static org.joda.time.Instant toJodaInstant(java.time.LocalDateTime localDateTime, java.time.ZoneId zone) {
|
||||||
return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone));
|
return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone));
|
||||||
}
|
}
|
||||||
|
|
||||||
// toJavaInstant
|
// toJavaInstant
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 {@link org.joda.time.Instant} 对象转换为 {@link java.time.Instant} 对象
|
||||||
|
*
|
||||||
|
* @param instant {@link org.joda.time.Instant} 对象
|
||||||
|
* @return {@link java.time.Instant} 对象
|
||||||
|
*/
|
||||||
public static java.time.Instant toJavaInstant(org.joda.time.Instant instant) {
|
public static java.time.Instant toJavaInstant(org.joda.time.Instant instant) {
|
||||||
return toInstant(instant.getMillis());
|
return toInstant(instant.getMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 joda-time 中的 {@link org.joda.time.DateTime} 对象转换为 Java 的
|
||||||
|
* {@link java.time.Instant} 对象
|
||||||
|
*
|
||||||
|
* @param dateTime joda-time 中表示日期时间的 {@link org.joda.time.DateTime} 对象
|
||||||
|
* @return Java 表示时间戳的 {@link java.time.Instant} 对象
|
||||||
|
*/
|
||||||
public static java.time.Instant toJavaInstant(org.joda.time.DateTime dateTime) {
|
public static java.time.Instant toJavaInstant(org.joda.time.DateTime dateTime) {
|
||||||
return toInstant(dateTime.getMillis());
|
return toInstant(dateTime.getMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 joda-time 中的 {@link org.joda.time.LocalDateTime} 对象和
|
||||||
|
* {@link org.joda.time.DateTimeZone} 对象
|
||||||
|
* 转换为 Java 中的 {@link java.time.Instant} 对象
|
||||||
|
*
|
||||||
|
* @param localDateTime
|
||||||
|
* @param zone
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public static java.time.Instant toJavaInstant(
|
public static java.time.Instant toJavaInstant(
|
||||||
org.joda.time.LocalDateTime localDateTime,
|
org.joda.time.LocalDateTime localDateTime,
|
||||||
org.joda.time.DateTimeZone zone) {
|
org.joda.time.DateTimeZone zone) {
|
||||||
|
@ -266,11 +462,28 @@ public class DateTimeTools {
|
||||||
|
|
||||||
// toJodaDateTime
|
// toJodaDateTime
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 Java 中表示日期时间的 {@link java.time.ZonedDateTime} 对象
|
||||||
|
* 转换为 joda-time 的 {@link org.joda.time.DateTime} 对象
|
||||||
|
*
|
||||||
|
* @param zonedDateTime 日期时间
|
||||||
|
* @return joda-time 中对应的 {@link org.joda.time.DateTime} 对象
|
||||||
|
*/
|
||||||
public static org.joda.time.DateTime toJodaDateTime(java.time.ZonedDateTime zonedDateTime) {
|
public static org.joda.time.DateTime toJodaDateTime(java.time.ZonedDateTime zonedDateTime) {
|
||||||
org.joda.time.DateTimeZone zone = org.joda.time.DateTimeZone.forID(zonedDateTime.getZone().getId());
|
org.joda.time.DateTimeZone zone = org.joda.time.DateTimeZone.forID(zonedDateTime.getZone().getId());
|
||||||
return toJodaInstant(zonedDateTime.toInstant()).toDateTime(zone);
|
return toJodaInstant(zonedDateTime.toInstant()).toDateTime(zone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 java.time 中表示日期时间的 {@link java.time.LocalDateTime} 对象和表示时区的
|
||||||
|
* {@link java.time.ZoneId} 对象转换为 joda-time 中对应的 {@link org.joda.time.DateTime}
|
||||||
|
* 对象
|
||||||
|
* 转换为 joda-time 中对应的 {@link org.joda.time.DateTime} 对象
|
||||||
|
*
|
||||||
|
* @param localDateTime 日期时间
|
||||||
|
* @param zone 时区
|
||||||
|
* @return joda-time 中对应的 {@link org.joda.time.DateTime} 对象
|
||||||
|
*/
|
||||||
public static org.joda.time.DateTime toJodaDateTime(
|
public static org.joda.time.DateTime toJodaDateTime(
|
||||||
java.time.LocalDateTime localDateTime,
|
java.time.LocalDateTime localDateTime,
|
||||||
java.time.ZoneId zone) {
|
java.time.ZoneId zone) {
|
||||||
|
@ -278,6 +491,13 @@ public class DateTimeTools {
|
||||||
return toJodaInstant(ZonedDateTime.of(localDateTime, zone).toInstant()).toDateTime(dateTimeZone);
|
return toJodaInstant(ZonedDateTime.of(localDateTime, zone).toInstant()).toDateTime(dateTimeZone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算时间戳在指定时区对应的时间,结果使用 {@link org.joda.time.DateTime} 表示
|
||||||
|
*
|
||||||
|
* @param instant java.time 中的时间戳
|
||||||
|
* @param zone java.time 中的时区
|
||||||
|
* @return joda-time 中带时区的日期时间
|
||||||
|
*/
|
||||||
public static org.joda.time.DateTime toJodaDateTime(
|
public static org.joda.time.DateTime toJodaDateTime(
|
||||||
java.time.Instant instant,
|
java.time.Instant instant,
|
||||||
java.time.ZoneId zone) {
|
java.time.ZoneId zone) {
|
||||||
|
@ -287,11 +507,26 @@ public class DateTimeTools {
|
||||||
|
|
||||||
// toZonedDateTime
|
// toZonedDateTime
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 joda-time 中带时区的日期时间,转换为 java.time 中带时区的日期时间
|
||||||
|
*
|
||||||
|
* @param dateTime joda-time 中带时区的日期时间
|
||||||
|
* @return java.time 中带时区的日期时间
|
||||||
|
*/
|
||||||
public static java.time.ZonedDateTime toZonedDateTime(org.joda.time.DateTime dateTime) {
|
public static java.time.ZonedDateTime toZonedDateTime(org.joda.time.DateTime dateTime) {
|
||||||
java.time.ZoneId zone = dateTime.getZone().toTimeZone().toZoneId();
|
java.time.ZoneId zone = dateTime.getZone().toTimeZone().toZoneId();
|
||||||
return toJavaInstant(dateTime.toInstant()).atZone(zone);
|
return toJavaInstant(dateTime.toInstant()).atZone(zone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 joda-time 中的 {@link org.joda.time.LocalDateTime} 和
|
||||||
|
* {@link org.joda.time.DateTimeZone}
|
||||||
|
* 转换为 java.time 中的 {@link java.time.ZonedDateTime}
|
||||||
|
*
|
||||||
|
* @param localDateTime joda-time 中的地区时间
|
||||||
|
* @param dateTimeZone joda-time 中的时区
|
||||||
|
* @return java.time 中带时区的日期时间
|
||||||
|
*/
|
||||||
public static java.time.ZonedDateTime toZonedDateTime(
|
public static java.time.ZonedDateTime toZonedDateTime(
|
||||||
org.joda.time.LocalDateTime localDateTime,
|
org.joda.time.LocalDateTime localDateTime,
|
||||||
org.joda.time.DateTimeZone dateTimeZone) {
|
org.joda.time.DateTimeZone dateTimeZone) {
|
||||||
|
@ -299,6 +534,14 @@ public class DateTimeTools {
|
||||||
return toJavaInstant(localDateTime, dateTimeZone).atZone(zone);
|
return toJavaInstant(localDateTime, dateTimeZone).atZone(zone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 joda-time 中的 {@link org.joda.time.Instant} 在指定时区的时间,用 Java 8
|
||||||
|
* {@link java.time.ZonedDateTime} 表示
|
||||||
|
*
|
||||||
|
* @param instant joda-time 中的时间戳
|
||||||
|
* @param dateTimeZone joda-time 中的时区
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public static java.time.ZonedDateTime toZonedDateTime(
|
public static java.time.ZonedDateTime toZonedDateTime(
|
||||||
org.joda.time.Instant instant,
|
org.joda.time.Instant instant,
|
||||||
org.joda.time.DateTimeZone dateTimeZone) {
|
org.joda.time.DateTimeZone dateTimeZone) {
|
||||||
|
@ -308,6 +551,12 @@ public class DateTimeTools {
|
||||||
|
|
||||||
// toJodaLocalDateTime
|
// toJodaLocalDateTime
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 {@link java.time.LocalDateTime} 转换为 {@link org.joda.time.LocalDateTime}
|
||||||
|
*
|
||||||
|
* @param localDateTime Java 8 LocalDateTime
|
||||||
|
* @return joda-time LocalDateTime
|
||||||
|
*/
|
||||||
public static org.joda.time.LocalDateTime toJodaLocalDateTime(java.time.LocalDateTime localDateTime) {
|
public static org.joda.time.LocalDateTime toJodaLocalDateTime(java.time.LocalDateTime localDateTime) {
|
||||||
java.time.ZoneId javaZone = java.time.ZoneId.systemDefault();
|
java.time.ZoneId javaZone = java.time.ZoneId.systemDefault();
|
||||||
org.joda.time.DateTimeZone jodaZone = toJodaTime(javaZone);
|
org.joda.time.DateTimeZone jodaZone = toJodaTime(javaZone);
|
||||||
|
@ -316,38 +565,104 @@ public class DateTimeTools {
|
||||||
|
|
||||||
// toJavaLocalDateTime
|
// toJavaLocalDateTime
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 {@link org.joda.time.LocalDateTime} 转换为 {@link java.time.LocalDateTime}
|
||||||
|
*
|
||||||
|
* @param localDateTime joda-time LocalDateTime
|
||||||
|
* @return Java 8 LocalDateTime
|
||||||
|
*/
|
||||||
public static java.time.LocalDateTime toJavaLocalDateTime(org.joda.time.LocalDateTime localDateTime) {
|
public static java.time.LocalDateTime toJavaLocalDateTime(org.joda.time.LocalDateTime localDateTime) {
|
||||||
org.joda.time.DateTimeZone jodaZone = org.joda.time.DateTimeZone.getDefault();
|
org.joda.time.DateTimeZone jodaZone = org.joda.time.DateTimeZone.getDefault();
|
||||||
java.time.ZoneId javaZone = toJavaZone(jodaZone);
|
java.time.ZoneId javaZone = toJavaZone(jodaZone);
|
||||||
return toJavaInstant(localDateTime, jodaZone).atZone(javaZone).toLocalDateTime();
|
return toJavaInstant(localDateTime, jodaZone).atZone(javaZone).toLocalDateTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换 Java API 和 joda-time API 表示时区的对象
|
||||||
|
*
|
||||||
|
* @param jodaZone joda-time API 中表示时区的对象
|
||||||
|
* @return Java API 中表示时区的对象
|
||||||
|
*/
|
||||||
public static java.time.ZoneId toJavaZone(org.joda.time.DateTimeZone jodaZone) {
|
public static java.time.ZoneId toJavaZone(org.joda.time.DateTimeZone jodaZone) {
|
||||||
return jodaZone.toTimeZone().toZoneId();
|
return jodaZone.toTimeZone().toZoneId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换 Java API 和 joda-time API 表示时区的对象
|
||||||
|
*
|
||||||
|
* @param zone Java API 中表示时区的对象
|
||||||
|
* @return joda-time API 中表示时区的对象
|
||||||
|
*/
|
||||||
public static org.joda.time.DateTimeZone toJodaTime(java.time.ZoneId zone) {
|
public static org.joda.time.DateTimeZone toJodaTime(java.time.ZoneId zone) {
|
||||||
return org.joda.time.DateTimeZone.forID(zone.getId());
|
return org.joda.time.DateTimeZone.forID(zone.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
// getQuarter
|
// getQuarter
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定日期所在季度
|
||||||
|
*
|
||||||
|
* @param date 日期
|
||||||
|
* @return 日期所在的季度
|
||||||
|
*/
|
||||||
public static YearQuarter getQuarter(Date date) {
|
public static YearQuarter getQuarter(Date date) {
|
||||||
return YearQuarter.of(date);
|
return YearQuarter.of(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定日期所在季度
|
||||||
|
*
|
||||||
|
* @param date 日期
|
||||||
|
* @return 日期所在的季度
|
||||||
|
*/
|
||||||
public static YearQuarter getQuarter(Calendar date) {
|
public static YearQuarter getQuarter(Calendar date) {
|
||||||
return YearQuarter.of(date);
|
return YearQuarter.of(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定月份所在季度
|
||||||
|
*
|
||||||
|
* @param month 月份
|
||||||
|
* @return 季度
|
||||||
|
*/
|
||||||
public static Quarter getQuarter(Month month) {
|
public static Quarter getQuarter(Month month) {
|
||||||
return Quarter.fromMonth(month);
|
return Quarter.fromMonth(month);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定年月所在季度
|
||||||
|
*
|
||||||
|
* @param year 年
|
||||||
|
* @param month 月
|
||||||
|
* @return 季度
|
||||||
|
*/
|
||||||
|
public static YearQuarter getQuarter(int year, Month month) {
|
||||||
|
return YearQuarter.of(YearMonth.of(year, month));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定年月所在季度
|
||||||
|
*
|
||||||
|
* @param yearMonth 年月
|
||||||
|
* @return 季度
|
||||||
|
*/
|
||||||
|
public static YearQuarter getQuarter(YearMonth yearMonth) {
|
||||||
|
return YearQuarter.of(yearMonth);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定日期所在季度
|
||||||
|
*
|
||||||
|
* @param date 日期
|
||||||
|
* @return 日期所在的季度
|
||||||
|
*/
|
||||||
public static YearQuarter getQuarter(LocalDate date) {
|
public static YearQuarter getQuarter(LocalDate date) {
|
||||||
return YearQuarter.of(date);
|
return YearQuarter.of(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 私有构造方法,明确标识该常量类的作用。
|
||||||
|
*/
|
||||||
private DateTimeTools() {
|
private DateTimeTools() {
|
||||||
throw new IllegalStateException("Utility class");
|
throw new IllegalStateException("Utility class");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue