2 Commits

Author SHA1 Message Date
808557e180 perf: 简单优化代码 2025-09-03 20:56:52 +08:00
4638853da6 refactor(exception)!: 重构多场景异常相关代码
- `IExceptionType` 不继承自 `IExceptionFactory`,具体表示异常场景的枚举,可按需实现这两个接口
- 简化 `IMultiTypesException` 接口定义,不与 `IExceptionType` 强制绑定
- 修改相关文档与描述

通过以上修改,使表示异常场景的枚举可以与异常类分开定义,使不同的异常可以复用同一套场景枚举。不强制作为单一异常的工厂,在被不同的异常复用时,可以更灵活地定义不同的工厂方法。
2025-08-01 17:30:36 +08:00
2 changed files with 13 additions and 28 deletions

View File

@@ -54,9 +54,7 @@ public class JodaTimeTools {
* @param zone 时区 * @param zone 时区
* @return {@link org.joda.time.Instant} 对象 * @return {@link org.joda.time.Instant} 对象
*/ */
public static org.joda.time.Instant toJodaInstant( public static org.joda.time.Instant toJodaInstant(java.time.LocalDateTime localDateTime, java.time.ZoneId zone) {
java.time.LocalDateTime localDateTime,
java.time.ZoneId zone) {
return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone)); return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone));
} }
@@ -94,9 +92,9 @@ public class JodaTimeTools {
* {@link org.joda.time.DateTimeZone} 对象 * {@link org.joda.time.DateTimeZone} 对象
* 转换为 Java 中的 {@link java.time.Instant} 对象 * 转换为 Java 中的 {@link java.time.Instant} 对象
* *
* @param localDateTime {@link org.joda.time.LocalDateTime} 对象 * @param localDateTime
* @param zone {@link org.joda.time.DateTimeZone} 对象 * @param zone
* @return Java 表示时间戳的 {@link java.time.Instant} 对象 * @return
*/ */
public static java.time.Instant toJavaInstant( public static java.time.Instant toJavaInstant(
org.joda.time.LocalDateTime localDateTime, org.joda.time.LocalDateTime localDateTime,
@@ -137,9 +135,8 @@ public class JodaTimeTools {
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) {
org.joda.time.LocalDateTime jodaLocalDateTime = toJodaLocalDateTime(localDateTime); org.joda.time.DateTimeZone dateTimeZone = toJodaZone(zone);
org.joda.time.DateTimeZone jodaZone = toJodaZone(zone); return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone).toInstant()).toDateTime(dateTimeZone);
return jodaLocalDateTime.toDateTime(jodaZone);
} }
/** /**
@@ -221,15 +218,9 @@ public class JodaTimeTools {
* @return joda-time 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) {
return new org.joda.time.LocalDateTime( java.time.ZoneId javaZone = java.time.ZoneId.systemDefault();
localDateTime.getYear(), org.joda.time.DateTimeZone jodaZone = toJodaZone(javaZone);
localDateTime.getMonthValue(), return toJodaInstant(localDateTime, javaZone).toDateTime(jodaZone).toLocalDateTime();
localDateTime.getDayOfMonth(),
localDateTime.getHour(),
localDateTime.getMinute(),
localDateTime.getSecond(),
localDateTime.getNano() / 1_000_000 // 毫秒转纳秒
);
} }
// ================================ // ================================
@@ -247,15 +238,9 @@ public class JodaTimeTools {
* @return Java 8 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) {
return java.time.LocalDateTime.of( org.joda.time.DateTimeZone jodaZone = org.joda.time.DateTimeZone.getDefault();
localDateTime.getYear(), java.time.ZoneId javaZone = toJavaZone(jodaZone);
localDateTime.getMonthOfYear(), return toJavaInstant(localDateTime, jodaZone).atZone(javaZone).toLocalDateTime();
localDateTime.getDayOfMonth(),
localDateTime.getHourOfDay(),
localDateTime.getMinuteOfHour(),
localDateTime.getSecondOfMinute(),
localDateTime.getMillisOfSecond() * 1_000_000 // 毫秒转纳秒
);
} }
// ================================ // ================================

View File

@@ -33,7 +33,7 @@ import org.junit.jupiter.api.Test;
public class JodaTimeToolsTests { public class JodaTimeToolsTests {
// Java // Java
static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2024, 12, 29, 12, 58, 30, 333 * 1_000_000); 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 LocalDate LOCAL_DATE = LOCAL_DATE_TIME.toLocalDate();
static final LocalTime LOCAL_TIME = LOCAL_DATE_TIME.toLocalTime(); static final LocalTime LOCAL_TIME = LOCAL_DATE_TIME.toLocalTime();