补充注释。

dev
ZhouXY108 2024-08-20 00:42:11 +08:00
parent c58e799b1e
commit 4ac281d65b
3 changed files with 461 additions and 12 deletions

View File

@ -7,22 +7,41 @@ import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.util.Numbers;
/**
*
*
* @author zhouxy
*/
public enum Quarter {
/** 第一季度 */
Q1(1, "Q1"),
/** 第二季度 */
Q2(2, "Q2"),
/** 第三季度 */
Q3(3, "Q3"),
/** 第四季度 */
Q4(4, "Q4"),
;
/** 季度值 (1/2/3/4) */
private final int value;
/** 季度名称 */
private final String displayName;
/** 季度开始月份 */
private final int startMonthValue;
/** 季度开始日期 */
private final MonthDay startMonthDay;
/** 季度结束月份 */
private final int lastMonthValue;
/** 季度结束日期 */
private final MonthDay lastMonthDay;
/**
* @param value (1/2/3/4)
* @param str
*/
Quarter(int value, String str) {
this.value = value;
this.displayName = str;
@ -36,20 +55,47 @@ public enum Quarter {
this.lastMonthDay = MonthDay.of(lastMonth, (value == 1 || value == 4) ? 31 : 30);
}
/**
*
*
* @param monthValue 112
* @return
* @throws IllegalArgumentException 112
*/
public static Quarter fromMonth(int monthValue) {
Preconditions.checkArgument(Numbers.between(monthValue, 1, 13), "Invalid value for MonthOfYear: " + monthValue);
return of(computeQuarterValueInternal(monthValue));
}
/**
*
*
* @param month
* @return
*/
public static Quarter fromMonth(Month month) {
final int monthValue = month.getValue();
return of(computeQuarterValueInternal(monthValue));
}
/**
* YearQuarter
*
*
* @param year
* @return YearQuarter
*/
public final YearQuarter atYear(int year) {
return YearQuarter.of(year, this);
}
/**
*
*
* @param value (1/2/3/4)
* @return
* @throws IllegalArgumentException 14
*/
public static Quarter of(int value) {
switch (value) {
case 1:
@ -65,6 +111,13 @@ public enum Quarter {
}
}
/**
*
*
* @param str
* @return
* @throws IllegalArgumentException Q1/Q2/Q3/Q4
*/
public static Quarter of(String str) {
switch (str) {
case "Q1":
@ -116,11 +169,13 @@ public enum Quarter {
// Getters end
// Internal
/**
*
*
*
* @param monthValue 1~12
* @return 1~4
* @param monthValue 112
* @return
*/
private static int computeQuarterValueInternal(int monthValue) {
return (monthValue - 1) / 3 + 1;

View File

@ -5,16 +5,29 @@ import java.time.Month;
import java.time.YearMonth;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import javax.annotation.Nonnull;
import com.google.common.base.Preconditions;
/**
*
*
* @author zhouxy
*/
public final class YearQuarter {
/** 年份 */
private final int year;
/** 季度 */
private final Quarter quarter;
/** 季度开始日期 */
private final LocalDate startDate;
/** 季度结束日期 */
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.");
this.year = year;
this.quarter = quarter;
@ -22,15 +35,34 @@ public final class YearQuarter {
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);
}
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()));
}
public static YearQuarter of(Date date) {
/**
* {@link YearQuarter}
*
* @param date
* @return {@link YearQuarter}
*/
public static YearQuarter of(@Nonnull Date date) {
@SuppressWarnings("deprecation")
final int year = date.getYear() + 1900;
@SuppressWarnings("deprecation")
@ -38,14 +70,28 @@ public final class YearQuarter {
return of(year, Quarter.fromMonth(month));
}
/**
* {@link YearQuarter}
*
* @param date
* @return {@link YearQuarter}
*/
public static YearQuarter of(Calendar date) {
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) {
return of(yearMonth.getYear(), Quarter.fromMonth(yearMonth.getMonth()));
}
// Getters
public int getYear() {
return year;
}
@ -77,4 +123,37 @@ public final class YearQuarter {
public LocalDate getLastDate() {
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;
}
}

View File

@ -5,6 +5,7 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
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.collection.MapWrapper;
/**
*
*
* @author zhouxy
*/
public class DateTimeTools {
/**
*
*/
private static final MapWrapper<String, DateTimeFormatter> DATE_TIME_FORMATTER_CACHE = MapWrapper
.<String, DateTimeFormatter>wrap(new SafeConcurrentHashMap<>())
.keyChecker(pattern -> Preconditions.checkArgument(StringUtils.isNotBlank(pattern), "The pattern could not be blank."))
.valueChecker(formatter -> Preconditions.checkNotNull(formatter, "The formatter could not be null."))
.build();
/**
*
*
* @param pattern
* @return
*/
public static DateTimeFormatter getDateTimeFormatter(String pattern) {
return DATE_TIME_FORMATTER_CACHE.computeIfAbsent(pattern, DateTimeFormatter::ofPattern);
}
/**
*
*
* @param pattern
* @param dateTime
* @return
*/
public static String toString(String pattern, ZonedDateTime dateTime) {
return getDateTimeFormatter(pattern).format(dateTime);
}
/**
* 使
*
* @param pattern
* @param instant
* @return
*/
public static String toString(String pattern, Instant instant) {
ZonedDateTime dateTime = instant.atZone(ZoneId.systemDefault());
return toString(pattern, dateTime);
}
/**
* 使
*
* @param pattern
* @param instant
* @param zone
* @return
*/
public static String toString(String pattern, Instant instant, ZoneId zone) {
ZonedDateTime dateTime = instant.atZone(zone);
return toString(pattern, dateTime);
}
/**
*
*
* @param pattern
* @return
*/
public static String nowStr(String pattern) {
return toString(pattern, ZonedDateTime.now());
}
/**
* 使
*
* @param pattern
* @param zone
* @return
*/
public static String nowStr(String pattern, ZoneId zone) {
return toString(pattern, Instant.now().atZone(zone));
}
// toDate
/**
* {@link Date}
*
* @param timeMillis
* @return {@link Date}
*/
public static Date toDate(long timeMillis) {
return Date.from(Instant.ofEpochMilli(timeMillis));
}
/**
* {@link Calendar} {@link Date}
*
* @param calendar {@link Calendar}
* @return {@link Date}
*/
public static Date toDate(Calendar calendar) {
return calendar.getTime();
}
/**
* {@link Instant} {@link Date}
*
* @param instant {@link Instant}
* @return {@link Date}
*/
public static Date toDate(Instant instant) {
return Date.from(instant);
}
/**
* {@link ZonedDateTime} {@link Date}
*
* @param zonedDateTime {@link ZonedDateTime}
* @return {@link Date}
*/
public static Date toDate(ZonedDateTime zonedDateTime) {
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) {
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) {
return Date.from(ZonedDateTime.of(localDate, localTime, zone).toInstant());
}
// toInstant
/**
* {@link Instant}
*
* @param timeMillis
* @return {@link Instant}
*/
public static Instant toInstant(long timeMillis) {
return Instant.ofEpochMilli(timeMillis);
}
/**
* {@link Date} {@link Instant}
*
* @param date {@link Date}
* @return {@link Instant}
*/
public static Instant toInstant(Date date) {
return date.toInstant();
}
/**
* {@link Calendar} {@link Instant}
*
* @param calendar {@link Calendar}
* @return {@link Instant}
*/
public static Instant toInstant(Calendar calendar) {
return calendar.toInstant();
}
/**
* {@link Instant}
* {@link ZonedDateTime} {@link Instant}
*
* @param zonedDateTime
* @return
*
* @deprecated 使 {@link ZonedDateTime#toInstant()}
* @param zonedDateTime {@link ZonedDateTime}
* @return {@link Instant}
* @deprecated 使 {@link ZonedDateTime#toInstant()}
*/
@Deprecated
public static Instant toInstant(ZonedDateTime zonedDateTime) { // NOSONAR
return zonedDateTime.toInstant();
}
/**
* 使 {@link LocalDateTime} {@link Instant}
*
* @param LocalDateTime {@link LocalDateTime}
* @param zone
* @return {@link Instant}
*/
public static Instant toInstant(LocalDateTime localDateTime, ZoneId zone) {
return ZonedDateTime.of(localDateTime, zone).toInstant();
}
@ -159,14 +272,35 @@ public class DateTimeTools {
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) {
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) {
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) {
return calendar.toInstant().atZone(zone.toZoneId());
}
@ -220,14 +354,35 @@ public class DateTimeTools {
return LocalDateTime.ofInstant(dateTime.toInstant(), timeZone.toZoneId());
}
/**
* {@link Calendar}
*
* @param calendar {@link Calendar}
* @param zone
* @return
*/
public static LocalDateTime toLocalDateTime(Calendar calendar, ZoneId zone) {
return LocalDateTime.ofInstant(calendar.toInstant(), zone);
}
/**
* {@link Calendar}
*
* @param calendar {@link Calendar}
* @param zone
* @return
*/
public static LocalDateTime toLocalDateTime(Calendar calendar, TimeZone zone) {
return LocalDateTime.ofInstant(calendar.toInstant(), zone.toZoneId());
}
/**
* {@link ZonedDateTime}
*
* @param zonedDateTime {@link ZonedDateTime}
* @param zone
* @return
*/
public static LocalDateTime toLocalDateTime(ZonedDateTime zonedDateTime, ZoneId zone) {
return LocalDateTime.ofInstant(zonedDateTime.toInstant(), zone);
}
@ -236,28 +391,69 @@ public class DateTimeTools {
// 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) {
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) {
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) {
return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone));
}
// 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) {
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) {
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(
org.joda.time.LocalDateTime localDateTime,
org.joda.time.DateTimeZone zone) {
@ -266,11 +462,28 @@ public class DateTimeTools {
// 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) {
org.joda.time.DateTimeZone zone = org.joda.time.DateTimeZone.forID(zonedDateTime.getZone().getId());
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(
java.time.LocalDateTime localDateTime,
java.time.ZoneId zone) {
@ -278,6 +491,13 @@ public class DateTimeTools {
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(
java.time.Instant instant,
java.time.ZoneId zone) {
@ -287,11 +507,26 @@ public class DateTimeTools {
// toZonedDateTime
/**
* joda-time java.time
*
* @param dateTime joda-time
* @return java.time
*/
public static java.time.ZonedDateTime toZonedDateTime(org.joda.time.DateTime dateTime) {
java.time.ZoneId zone = dateTime.getZone().toTimeZone().toZoneId();
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(
org.joda.time.LocalDateTime localDateTime,
org.joda.time.DateTimeZone dateTimeZone) {
@ -299,6 +534,14 @@ public class DateTimeTools {
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(
org.joda.time.Instant instant,
org.joda.time.DateTimeZone dateTimeZone) {
@ -308,6 +551,12 @@ public class DateTimeTools {
// 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) {
java.time.ZoneId javaZone = java.time.ZoneId.systemDefault();
org.joda.time.DateTimeZone jodaZone = toJodaTime(javaZone);
@ -316,38 +565,104 @@ public class DateTimeTools {
// 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) {
org.joda.time.DateTimeZone jodaZone = org.joda.time.DateTimeZone.getDefault();
java.time.ZoneId javaZone = toJavaZone(jodaZone);
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) {
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) {
return org.joda.time.DateTimeZone.forID(zone.getId());
}
// getQuarter
/**
*
*
* @param date
* @return
*/
public static YearQuarter getQuarter(Date date) {
return YearQuarter.of(date);
}
/**
*
*
* @param date
* @return
*/
public static YearQuarter getQuarter(Calendar date) {
return YearQuarter.of(date);
}
/**
*
*
* @param month
* @return
*/
public static Quarter getQuarter(Month 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) {
return YearQuarter.of(date);
}
/**
*
*/
private DateTimeTools() {
throw new IllegalStateException("Utility class");
}