refactor!: 将 JodaTime 相关方法从 DateTimeTools 类中提取到新的 JodaTimeTools 类
This commit is contained in:
@@ -374,258 +374,6 @@ public class DateTimeTools {
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - 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));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - 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) {
|
||||
return toJavaInstant(localDateTime.toDateTime(zone));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - 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) {
|
||||
org.joda.time.DateTimeZone dateTimeZone = toJodaZone(zone);
|
||||
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) {
|
||||
org.joda.time.DateTimeZone dateTimeZone = toJodaZone(zone);
|
||||
return toJodaInstant(instant).toDateTime(dateTimeZone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - 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) {
|
||||
java.time.ZoneId zone = toJavaZone(dateTimeZone);
|
||||
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) {
|
||||
java.time.ZoneId zone = toJavaZone(dateTimeZone);
|
||||
return toJavaInstant(instant).atZone(zone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - 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 = toJodaZone(javaZone);
|
||||
return toJodaInstant(localDateTime, javaZone).toDateTime(jodaZone).toLocalDateTime();
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - 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();
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 转换 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 toJodaZone(java.time.ZoneId zone) {
|
||||
return org.joda.time.DateTimeZone.forID(zone.getId());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - YearQuarter & Quarter
|
||||
// ================================
|
||||
|
@@ -0,0 +1,284 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
/**
|
||||
* Joda-Time 工具类
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
public class JodaTimeTools {
|
||||
|
||||
// ================================
|
||||
// #region - 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));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - 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 DateTimeTools.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 DateTimeTools.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) {
|
||||
return toJavaInstant(localDateTime.toDateTime(zone));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - 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) {
|
||||
org.joda.time.DateTimeZone dateTimeZone = toJodaZone(zone);
|
||||
return toJodaInstant(java.time.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) {
|
||||
org.joda.time.DateTimeZone dateTimeZone = toJodaZone(zone);
|
||||
return toJodaInstant(instant).toDateTime(dateTimeZone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - 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) {
|
||||
java.time.ZoneId zone = toJavaZone(dateTimeZone);
|
||||
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) {
|
||||
java.time.ZoneId zone = toJavaZone(dateTimeZone);
|
||||
return toJavaInstant(instant).atZone(zone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - 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 = toJodaZone(javaZone);
|
||||
return toJodaInstant(localDateTime, javaZone).toDateTime(jodaZone).toLocalDateTime();
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - 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();
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 转换 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 toJodaZone(java.time.ZoneId zone) {
|
||||
return org.joda.time.DateTimeZone.forID(zone.getId());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 私有构造方法
|
||||
*/
|
||||
private JodaTimeTools() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
@@ -277,144 +277,6 @@ class DateTimeToolsTests {
|
||||
// #endregion - toLocalDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaInstant
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaInstant_JavaInstant() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, DateTimeTools.toJodaInstant(INSTANT_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_INSTANT, DateTimeTools.toJodaInstant(INSTANT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaInstant_ZonedDateTime() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, DateTimeTools.toJodaInstant(ZONED_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_INSTANT, DateTimeTools.toJodaInstant(ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaInstant_LocalDateTimeAndZoneId() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, DateTimeTools.toJodaInstant(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(JODA_INSTANT, DateTimeTools.toJodaInstant(LOCAL_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaInstant
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJavaInstant
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaInstant() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toJavaInstant(JODA_INSTANT_WITH_SYS_ZONE));
|
||||
assertEquals(INSTANT, DateTimeTools.toJavaInstant(JODA_INSTANT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaDateTime() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toJavaInstant(JODA_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(INSTANT, DateTimeTools.toJavaInstant(JODA_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaLocalDateTimeAndJodaDateTimeZone() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toJavaInstant(JODA_LOCAL_DATE_TIME, JODA_SYS_ZONE));
|
||||
assertEquals(INSTANT, DateTimeTools.toJavaInstant(JODA_LOCAL_DATE_TIME, JODA_ZONE));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJavaInstant
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_ZonedDateTime() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toJodaDateTime(ZONED_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_DATE_TIME, DateTimeTools.toJodaDateTime(ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_LocalDateTimeAndZoneId() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toJodaDateTime(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(JODA_DATE_TIME, DateTimeTools.toJodaDateTime(LOCAL_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_InstantAndZoneId() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toJodaDateTime(INSTANT_WITH_SYS_ZONE, SYS_ZONE_ID));
|
||||
assertEquals(JODA_DATE_TIME, DateTimeTools.toJodaDateTime(INSTANT, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaDateTime() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(JODA_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(JODA_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaLocalDateTimeAndJodaDateTimeZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(JODA_LOCAL_DATE_TIME, JODA_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(JODA_LOCAL_DATE_TIME, JODA_ZONE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaInstantAndJodaDateTimeZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(JODA_INSTANT_WITH_SYS_ZONE, JODA_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(JODA_INSTANT, JODA_ZONE));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaLocalDateTime_JavaLocalDateTime() {
|
||||
assertEquals(JODA_LOCAL_DATE_TIME, DateTimeTools.toJodaLocalDateTime(LOCAL_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaLocalDateTime_JodaLocalDateTime() {
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toJavaLocalDateTime(JODA_LOCAL_DATE_TIME));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void convertJavaZoneIdAndJodaDateTimeZone() {
|
||||
assertEquals(SYS_ZONE_ID, DateTimeTools.toJavaZone(JODA_SYS_ZONE));
|
||||
assertEquals(ZONE_ID, DateTimeTools.toJavaZone(JODA_ZONE));
|
||||
assertEquals(JODA_SYS_ZONE, DateTimeTools.toJodaZone(SYS_ZONE_ID));
|
||||
assertEquals(JODA_ZONE, DateTimeTools.toJodaZone(ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - YearQuarter & Quarter
|
||||
// ================================
|
||||
|
@@ -0,0 +1,223 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class JodaTimeToolsTests {
|
||||
|
||||
// Java
|
||||
static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2024, 12, 29, 12, 58, 30, 333000000);
|
||||
static final LocalDate LOCAL_DATE = LOCAL_DATE_TIME.toLocalDate();
|
||||
static final LocalTime LOCAL_TIME = LOCAL_DATE_TIME.toLocalTime();
|
||||
|
||||
// Java - 2024-12-29 12:58:30.333333333 SystemDefaultZone
|
||||
static final ZoneId SYS_ZONE_ID = ZoneId.systemDefault();
|
||||
static final ZonedDateTime ZONED_DATE_TIME_WITH_SYS_ZONE = LOCAL_DATE_TIME.atZone(SYS_ZONE_ID);
|
||||
static final Instant INSTANT_WITH_SYS_ZONE = ZONED_DATE_TIME_WITH_SYS_ZONE.toInstant();
|
||||
static final long INSTANT_MILLIS = INSTANT_WITH_SYS_ZONE.toEpochMilli();
|
||||
|
||||
static final TimeZone SYS_TIME_ZONE = TimeZone.getDefault();
|
||||
static final Date SYS_DATE = Date.from(INSTANT_WITH_SYS_ZONE);
|
||||
static final Calendar SYS_CALENDAR = Calendar.getInstance(SYS_TIME_ZONE);
|
||||
static {
|
||||
SYS_CALENDAR.setTime(SYS_DATE);
|
||||
}
|
||||
|
||||
// Java - 2024-12-29 12:58:30.333333333 GMT+04:00
|
||||
static final ZoneId ZONE_ID = ZoneId.of("GMT+04:00");
|
||||
static final ZonedDateTime ZONED_DATE_TIME = LOCAL_DATE_TIME.atZone(ZONE_ID);
|
||||
static final Instant INSTANT = ZONED_DATE_TIME.toInstant();
|
||||
static final long MILLIS = INSTANT.toEpochMilli();
|
||||
|
||||
static final TimeZone TIME_ZONE = TimeZone.getTimeZone(ZONE_ID);
|
||||
static final Date DATE = Date.from(INSTANT);
|
||||
static final Calendar CALENDAR = Calendar.getInstance(TIME_ZONE);
|
||||
static {
|
||||
CALENDAR.setTime(DATE);
|
||||
}
|
||||
|
||||
// Joda
|
||||
static final org.joda.time.LocalDateTime JODA_LOCAL_DATE_TIME
|
||||
= new org.joda.time.LocalDateTime(2024, 12, 29, 12, 58, 30, 333);
|
||||
static final org.joda.time.LocalDate JODA_LOCAL_DATE = JODA_LOCAL_DATE_TIME.toLocalDate();
|
||||
static final org.joda.time.LocalTime JODA_LOCAL_TIME = JODA_LOCAL_DATE_TIME.toLocalTime();
|
||||
|
||||
// Joda - 2024-12-29 12:58:30.333 SystemDefaultZone
|
||||
static final org.joda.time.DateTimeZone JODA_SYS_ZONE = org.joda.time.DateTimeZone.getDefault();
|
||||
static final org.joda.time.DateTime JODA_DATE_TIME_WITH_SYS_ZONE = JODA_LOCAL_DATE_TIME.toDateTime(JODA_SYS_ZONE);
|
||||
static final org.joda.time.Instant JODA_INSTANT_WITH_SYS_ZONE = JODA_DATE_TIME_WITH_SYS_ZONE.toInstant();
|
||||
static final long JODA_INSTANT_MILLIS = JODA_INSTANT_WITH_SYS_ZONE.getMillis();
|
||||
|
||||
// Joda - 2024-12-29 12:58:30.333 GMT+04:00
|
||||
static final org.joda.time.DateTimeZone JODA_ZONE = org.joda.time.DateTimeZone.forID("GMT+04:00");
|
||||
static final org.joda.time.DateTime JODA_DATE_TIME = JODA_LOCAL_DATE_TIME.toDateTime(JODA_ZONE);
|
||||
static final org.joda.time.Instant JODA_INSTANT = JODA_DATE_TIME.toInstant();
|
||||
static final long JODA_MILLIS = JODA_INSTANT.getMillis();
|
||||
|
||||
|
||||
// ================================
|
||||
// #region - toJodaInstant
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaInstant_JavaInstant() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, JodaTimeTools.toJodaInstant(INSTANT_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_INSTANT, JodaTimeTools.toJodaInstant(INSTANT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaInstant_ZonedDateTime() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, JodaTimeTools.toJodaInstant(ZONED_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_INSTANT, JodaTimeTools.toJodaInstant(ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaInstant_LocalDateTimeAndZoneId() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, JodaTimeTools.toJodaInstant(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(JODA_INSTANT, JodaTimeTools.toJodaInstant(LOCAL_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaInstant
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJavaInstant
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaInstant() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, JodaTimeTools.toJavaInstant(JODA_INSTANT_WITH_SYS_ZONE));
|
||||
assertEquals(INSTANT, JodaTimeTools.toJavaInstant(JODA_INSTANT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaDateTime() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, JodaTimeTools.toJavaInstant(JODA_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(INSTANT, JodaTimeTools.toJavaInstant(JODA_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaLocalDateTimeAndJodaDateTimeZone() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, JodaTimeTools.toJavaInstant(JODA_LOCAL_DATE_TIME, JODA_SYS_ZONE));
|
||||
assertEquals(INSTANT, JodaTimeTools.toJavaInstant(JODA_LOCAL_DATE_TIME, JODA_ZONE));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJavaInstant
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_ZonedDateTime() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, JodaTimeTools.toJodaDateTime(ZONED_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_DATE_TIME, JodaTimeTools.toJodaDateTime(ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_LocalDateTimeAndZoneId() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, JodaTimeTools.toJodaDateTime(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(JODA_DATE_TIME, JodaTimeTools.toJodaDateTime(LOCAL_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_InstantAndZoneId() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, JodaTimeTools.toJodaDateTime(INSTANT_WITH_SYS_ZONE, SYS_ZONE_ID));
|
||||
assertEquals(JODA_DATE_TIME, JodaTimeTools.toJodaDateTime(INSTANT, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaDateTime() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, JodaTimeTools.toZonedDateTime(JODA_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, JodaTimeTools.toZonedDateTime(JODA_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaLocalDateTimeAndJodaDateTimeZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, JodaTimeTools.toZonedDateTime(JODA_LOCAL_DATE_TIME, JODA_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, JodaTimeTools.toZonedDateTime(JODA_LOCAL_DATE_TIME, JODA_ZONE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaInstantAndJodaDateTimeZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, JodaTimeTools.toZonedDateTime(JODA_INSTANT_WITH_SYS_ZONE, JODA_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, JodaTimeTools.toZonedDateTime(JODA_INSTANT, JODA_ZONE));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaLocalDateTime_JavaLocalDateTime() {
|
||||
assertEquals(JODA_LOCAL_DATE_TIME, JodaTimeTools.toJodaLocalDateTime(LOCAL_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaLocalDateTime_JodaLocalDateTime() {
|
||||
assertEquals(LOCAL_DATE_TIME, JodaTimeTools.toJavaLocalDateTime(JODA_LOCAL_DATE_TIME));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void convertJavaZoneIdAndJodaDateTimeZone() {
|
||||
assertEquals(SYS_ZONE_ID, JodaTimeTools.toJavaZone(JODA_SYS_ZONE));
|
||||
assertEquals(ZONE_ID, JodaTimeTools.toJavaZone(JODA_ZONE));
|
||||
assertEquals(JODA_SYS_ZONE, JodaTimeTools.toJodaZone(SYS_ZONE_ID));
|
||||
assertEquals(JODA_ZONE, JodaTimeTools.toJodaZone(ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
}
|
Reference in New Issue
Block a user