补充注释。

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; 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 112
* @return
* @throws IllegalArgumentException 112
*/
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 14
*/
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 112
* @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;

View File

@ -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;
}
} }

View File

@ -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");
} }