LocalDate()指定年月日构造LocalDate对象
private LocalDate(int year, int month, int dayOfMonth) {
this.year = year;
this.month = (short) month;
this.day = (short) dayOfMonth;
}
now(),有三个重载,分别时默认当前时区当前日期,指定时区的当前日期,指定时间间隔的时钟
public static LocalDate now() {
return now(Clock.systemDefaultZone());
}
public static LocalDate now(ZoneId zone) {
return now(Clock.system(zone));
}
public static LocalDate now(Clock clock) {
Objects.requireNonNull(clock, "clock");
// inline to avoid creating object and Instant checks
final Instant now = clock.instant(); // called once
ZoneOffset offset = clock.getZone().getRules().getOffset(now);
// overflow caught later
long epochSec = now.getEpochSecond() + offset.getTotalSeconds();
long epochDay = Math.floorDiv(epochSec, SECONDS_PER_DAY);
return LocalDate.ofEpochDay(epochDay);
}
parse(),两个重载,解析字符转为LocalDate
public static LocalDate parse(CharSequence text) {
return parse(text, DateTimeFormatter.ISO_LOCAL_DATE);
}
public static LocalDate parse(CharSequence text, DateTimeFormatter formatter) {
Objects.requireNonNull(formatter, "formatter");
return formatter.parse(text, LocalDate::from);
}
get(),有多种get(),可以获取日期字段
public int getYear() {
return year;
}
public Month getMonth() {
return Month.of(month);
}
plus(),有多种plus(),按指定值加上当前日期
public LocalDate plusDays(long daysToAdd) {
if (daysToAdd == 0) {
return this;
}
long mjDay = Math.addExact(toEpochDay(), daysToAdd);
return LocalDate.ofEpochDay(mjDay);
}
minus(),有多种minus(),按当前日期减去的指定值
public LocalDate minusYears(long yearsToSubtract) {
return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract));
}
format(),按指定规则格式化转换日期
public String format(DateTimeFormatter formatter) {
Objects.requireNonNull(formatter, "formatter");
return formatter.format(this);
}