自定义 temporaladjuster 的核心是实现 java.time.temporal.temporaladjuster 接口,并重写其唯一的 adjustinto(temporal) 方法。在该方法中,你需要接收一个可修改的时间对象(如 temporalaccessor),利用 temporalqueries 或类型转换获取具体的日期字段,计算目标值后返回一个新的时间对象。这种方式允许你精确控制年、月、日、时、分、秒等任何时间单位的调整逻辑,且能保持不可变对象的线程安全性。

adjustInto 方法如何接收并处理时间对象
在实现 adjustInto 时,参数 temporal 是一个泛型接口,通常传入的是 LocalDate 或 LocalDateTime 的实例。你需要判断该对象是否支持所需的时间字段,例如使用 temporal.isSupported(ChronoField.DAY_OF_MONTH)。接着,通过 temporal.get(ChronoField.XXX) 读取当前值,进行数学运算得到新值,最后调用 temporal.with(ChronoField.XXX, newValue) 生成并返回新的时间对象。这个过程不修改原对象,而是创建副本。

import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalQuery;
public class CustomAdjuster implements TemporalAdjuster {
private final int targetDay;
public CustomAdjuster(int targetDay) {
this.targetDay = targetDay;
}
@Override
public Temporal adjustInto(Temporal temporal) {
// 检查是否支持日期字段
if (!temporal.isSupported(ChronoField.DAY_OF_MONTH)) {
throw new UnsupportedTemporalTypeException("Unsupported field: Day of Month");
}
// 获取当前月份和年份,以便构建新日期
int year = temporal.get(ChronoField.YEAR);
int month = temporal.get(ChronoField.MONTH_OF_YEAR);
// 创建新的 LocalDate 实例,设置为目标日期
LocalDate date = LocalDate.of(year, month, targetDay);
// 如果目标日期超出当月天数(例如3月31日放入2月),会自动修正为月末
// 这里我们强制设置为指定日,若无效则抛出异常或根据需求处理
try {
return date.atStartOfDay();
} catch (Exception e) {
// 简单处理:如果目标日不存在,回退到月末
int maxDay = date.lengthOfMonth();
return LocalDate.of(year, month, maxDay).atStartOfDay();
}
}
}
如何将自定义 Adjuster 应用到现有日期
一旦实现了 TemporalAdjuster,就可以通过 LocalDate 或 LocalDateTime 的 with() 方法将其应用到一个具体的日期实例上。with() 方法会调用 Adjuster 的 adjustInto 方法,并返回调整后的新对象。你可以将自定义的 Adjuster 作为参数直接传递,或者将其封装在静态工厂方法中以供复用。这种链式调用方式使得代码非常清晰,且符合函数式编程的风格。
一个基于源码的 OpenAI Agents SDK JavaScript 和 TypeScript 技术文档。涵盖了代理编排、工具、转接、追踪以及实际安装路径。
立即学习“Java免费学习笔记(深入)”;

import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
public class Main {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println("Today: " + today);
// 创建自定义 Adjuster 实例,设定目标日为每月15号
TemporalAdjuster to15th = new CustomAdjuster(15);
// 应用调整器
LocalDate adjustedDate = today.with(to15th);
System.out.println("Adjusted to 15th: " + adjustedDate);
// 另一种写法:直接在 with 中实现逻辑(匿名内部类)
TemporalAdjuster toNextMonthFirst = temporal -> {
int year = temporal.get(ChronoField.YEAR);
int month = temporal.get(ChronoField.MONTH_OF_YEAR) + 1;
if (month > 12) {
year++;
month = 1;
}
return LocalDate.of(year, month, 1);
};
LocalDate nextMonthFirst = today.with(toNextMonthFirst);
System.out.println("Next Month First: " + nextMonthFirst);
}
}
// 复用上面的 CustomAdjuster 类定义
class CustomAdjuster implements TemporalAdjuster {
private final int targetDay;
public CustomAdjuster(int targetDay) {
this.targetDay = targetDay;
}
@Override
public Temporal adjustInto(Temporal temporal) {
if (!temporal.isSupported(ChronoField.DAY_OF_MONTH)) {
throw new UnsupportedTemporalTypeException("Unsupported field: Day of Month");
}
int year = temporal.get(ChronoField.YEAR);
int month = temporal.get(ChronoField.MONTH_OF_YEAR);
LocalDate date = LocalDate.of(year, month, targetDay);
try {
return date.atStartOfDay();
} catch (Exception e) {
int maxDay = date.lengthOfMonth();
return LocalDate.of(year, month, maxDay).atStartOfDay();
}
}
}
当目标日期在当前月份不存在时如何处理边界情况
在自定义调整逻辑中,最容易被忽视的是边界情况。例如,如果用户希望将日期调整为每月的“31号”,但当前月份只有30天(如4月),直接创建 LocalDate.of(year, month, 31) 会抛出 DateTimeException。因此,在 adjustInto 的实现中,必须包含错误处理机制。常见的策略包括:自动回退到该月的最后一天、抛出明确异常告知调用者、或者静默忽略并保留原日期。上述代码示例中采用了回退到月末的策略,这在处理账单周期或月度报告时非常实用。开发者应根据具体业务场景选择合适的边界处理逻辑,并在文档中明确说明。

















