(一)LocalDate
方法 | 描述 |
---|---|
MAX | 最大日期 |
MIN | 最小日期 |
static LocalDate now() | 获取系统当前时间 |
static LocalDate parse(CharSequence text) static LocalDate parse(CharSequence text, DateTimeFormatter formatter) |
将日期字符串转换成LocalDate类型 |
Temporal adjustInto(Temporal temporal) | 将传入对象与该对象有相同的时间, 不改变原有LocalDate |
LocalDateTime atStartOfDay() ZonedDateTime atStartOfDay(ZoneId zone) |
获取当前日期的开始时间 |
LocalDateTime atTime(LocalTime time) LocalDateTime atTime(int hour, int minute) LocalDateTime atTime(int hour, int minute, int LocalDateTime second) LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond) |
LocalDate添加LocalTime,变成LocalDateTime |
String format(DateTimeFormatter formatter) | 格式化 |
static LocalDate from(TemporalAccessor temporal) | 从指定对象获取LocalDate |
int get(TemporalField field) long getLong(TemporalField field) |
获取当前日期中某个字段值返回int类型 获取当前日期中某个字段值返回long类型 |
IsoChronology getChronology() | 获取当前日期的IsoChronology |
int getDayOfMonth() DayOfWeek getDayOfWeek() int getDayOfYear() Month getMonth() int getMonthValue() int lengthOfMonth() int getYear() int lengthOfYear() |
获取当前日期是所在月的第几天 获取当前日期是星期几(星期的英文全称) 获取当前日期是所在年的第几天 获取当前日期所在月份(月份的英文全称) 获取当前日期所在月份的数值 获取当前日期所在月份有多少天 获取当前日期所在年份的数值 获取当前日期所在年份有多少天 |
boolean isLeapYear() boolean isAfter(ChronoLocalDate other) boolean isBefore(ChronoLocalDate other) boolean isEqual(ChronoLocalDate other) |
判断当前日期所在年是否是闰年 判断当前日期是否在指定日期之后 判断当前日期是否在指定日期之前 判断当前日期是否和指定日期相等 |
Era getEra() | 获取当前对象的时代 |
boolean isSupported(TemporalField field) boolean isSupported(TemporalUnit unit) |
判断当前日期对象是否有对应字段 |
LocalDate minus(TemporalAmount amountToSubtract) LocalDate minus(long amountToSubtract, TemporalUnit unit) LocalDate plus(TemporalAmount amountToAdd) LocalDate plus(long amountToAdd, TemporalUnit unit) |
对当前日期对象进行增减 |
LocalDate minusDays(long daysToSubtract) LocalDate minusWeeks(long weeksToSubtract) LocalDate minusMonths(long monthsToSubtract) LocalDate minusYears(long yearsToSubtract) LocalDate plusDays(long daysToAdd) LocalDate plusWeeks(long weeksToAdd) LocalDate plusMonths(long monthsToAdd) LocalDate plusYears(long yearsToAdd) |
对当前日期对象指定字段进行增减 |
static LocalDate of(int year, int month, int dayOfMonth) static LocalDate of(int year, Month month, int dayOfMonth) static LocalDate ofEpochDay(long epochDay) static LocalDate ofYearDay(int year, int dayOfYear) |
创建LocalDate |
long toEpochDay() | 获取当前日期对象到1970-01-01的天数 |
Period until(ChronoLocalDate endDateExclusive) long until(Temporal endExclusive, TemporalUnit unit) |
获取当前日期对象到指定日期的间隔 |
LocalDate with(TemporalAdjuster adjuster) LocalDate withDayOfMonth(int dayOfMonth) LocalDate withDayOfYear(int dayOfYear) LocalDate withMonth(int month) LocalDate withYear(int year) |
调整日期 |
eg:
//最大日期 +999999999-12-31
System.out.println("MAX: " + LocalDate.MAX);
System.out.println("-----------------");
//最小日期
System.out.println("MIN: " + LocalDate.MIN);
System.out.println("-----------------");
//获取系统当前时间
LocalDate now = LocalDate.now();
System.out.println("now: " + now);
System.out.println("-----------------");
//将日期字符串转换成LocalDate类型
LocalDate localDate1 = LocalDate.parse("2020-09-10");
LocalDate localDate2 = LocalDate.parse("2020:09:15", DateTimeFormatter.ofPattern("yyyy:MM:dd"));
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("-----------------");
//将传入对象与该对象有相同的时间, 不改变原有LocalDate
System.out.println("localDate2 adjustInto localDate1: " + localDate1.adjustInto(localDate2));
System.out.println("localDate2: " + localDate2);
System.out.println("-----------------");
//获取当前日期的开始时间,
System.out.println("localDate1 Start: " + localDate1.atStartOfDay());
ZoneId zoneId = ZoneId.of("Africa/Accra");
System.out.println("localDate1 Start: " + localDate1.atStartOfDay(zoneId));
System.out.println("-----------------");
//LocalDate添加LocalTime,变成LocalDateTime
System.out.println("localDate1 LocalDateTime: " + localDate1.atTime(LocalTime.now()));
System.out.println("localDate1 LocalDateTime: " + localDate1.atTime(12, 10));
System.out.println("localDate1 LocalDateTime: " + localDate1.atTime(12, 10, 30));
System.out.println("-----------------");
//格式化
DateTimeFormatter f1 = DateTimeFormatter.ofPattern("yyyy");
DateTimeFormatter f2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println("localDate1 format yyyy: " + localDate1.format(f1));
System.out.println("localDate1 format yyyy-MM-dd: " + localDate1.format(f2));
System.out.println("-----------------");
//从指定对象获取LocalDate
System.out.println("from LocalDateTime now: " + LocalDate.from(LocalDateTime.now()));
System.out.println("from LocalDate now: " + LocalDate.from(LocalDate.now()));
System.out.println("from ZonedDateTime now: " + LocalDate.from(ZonedDateTime.now()));
System.out.println("-----------------");
//获取当前日期中某个字段值返回int类型
System.out.println("localDate1 get int YEAR: " + localDate1.get(ChronoField.YEAR));
System.out.println("localDate1 get int MONTH_OF_YEAR: " + localDate1.get(ChronoField.MONTH_OF_YEAR));
System.out.println("localDate1 get int DAY_OF_YEAR: " + localDate1.get(ChronoField.DAY_OF_YEAR));
System.out.println("localDate1 get int DAY_OF_WEEK: " + localDate1.get(ChronoField.DAY_OF_WEEK));
//获取当前日期中某个字段值返回long类型
System.out.println("localDate1 get long YEAR: " + localDate1.getLong(ChronoField.YEAR));
System.out.println("localDate1 get long MONTH_OF_YEAR: " + localDate1.getLong(ChronoField.MONTH_OF_YEAR));
System.out.println("localDate1 get long DAY_OF_YEAR: " + localDate1.getLong(ChronoField.DAY_OF_YEAR));
System.out.println("localDate1 get long DAY_OF_WEEK: " + localDate1.getLong(ChronoField.DAY_OF_WEEK));
System.out.println("-----------------");
//获取当前日期的IsoChronology
System.out.println("localDate1 IsoChronology: " + localDate1.getChronology());
System.out.println("-----------------");
//获取当前日期是所在月的第几天
System.out.println("localDate1 DayOfMonth: " + localDate1.getDayOfMonth());
//获取当前日期是星期几(星期的英文全称)
System.out.println("localDate1 DayOfWeek: " + localDate1.getDayOfWeek());
//获取当前日期是所在年的第几天
System.out.println("localDate1 DayOfYear: " + localDate1.getDayOfYear());
//获取当前日期所在月份(月份的英文全称)
System.out.println("localDate1 Month: " + localDate1.getMonth());
//获取当前日期所在月份的数值
System.out.println("localDate1 MonthValue: " + localDate1.getMonthValue());
//获取当前日期所在月份有多少天
System.out.println("localDate1 lengthOfMonth: " + localDate1.lengthOfMonth());
//获取当前日期所在年份的数值
System.out.println("localDate1 Year: " + localDate1.getYear());
//获取当前日期所在年份有多少天
System.out.println("localDate1 lengthOfYear: " + localDate1.lengthOfYear());
System.out.println("-----------------");
//判断当前日期所在年是否是闰年
System.out.println("localDate1 isLeapYear: " + localDate1.isLeapYear());
//判断当前日期是否在指定日期之后
System.out.println("localDate1 isAfter localDate2: " + localDate1.isAfter(localDate2));
//判断当前日期是否在指定日期之前
System.out.println("localDate1 isBefore localDate2: " + localDate1.isBefore(localDate2));
//判断当前日期是否和指定日期相等
System.out.println("localDate1 isEqual localDate2: " + localDate1.isEqual(localDate2));
System.out.println("-----------------");
//获取当前日期对象的时代
System.out.println("localDate1 Era: " + localDate1.getEra());
System.out.println("-----------------");
//判断当前日期对象是否有对应字段
System.out.println("localDate1 isSupported DAY_OF_WEEK: " + localDate1.isSupported(ChronoField.DAY_OF_WEEK));
System.out.println("localDate1 isSupported MINUTE_OF_DAY: " + localDate1.isSupported(ChronoField.MINUTE_OF_DAY));
System.out.println("localDate1 isSupported DECADES: " + localDate1.isSupported(ChronoUnit.DECADES));
System.out.println("-----------------");
//对当前日期对象进行增减
Period weeks = Period.ofWeeks(2);
System.out.println("localDate1 minus weeks: " + localDate1.minus(weeks));
System.out.println("localDate1 minus DAYS: " + localDate1.minus(1, ChronoUnit.DAYS));
System.out.println("localDate1 minus weeks: " + localDate1.plus(weeks));
System.out.println("localDate1 minus DAYS: " + localDate1.plus(1, ChronoUnit.DAYS));
//对当前日期对象指定字段进行增减
System.out.println("localDate1 minus Days: " + localDate1.minusDays(1));
System.out.println("localDate1 minus weeks: " + localDate1.minusWeeks(1));
System.out.println("localDate1 minus Months: " + localDate1.minusMonths(1));
System.out.println("localDate1 minus Years: " + localDate1.minusYears(1));
System.out.println("localDate1 plus Days: " + localDate1.plusDays(1));
System.out.println("localDate1 plus weeks: " + localDate1.plusWeeks(1));
System.out.println("localDate1 plus Months: " + localDate1.plusMonths(1));
System.out.println("localDate1 plus Years: " + localDate1.plusYears(1));
System.out.println("-----------------");
//创建LocalDate
System.out.println("LocalDate of: " + LocalDate.of(2020, 9, 16));
System.out.println("LocalDate of: " + LocalDate.of(2020, Month.APRIL, 15));
System.out.println("LocalDate ofEpochDay: " + LocalDate.ofEpochDay(15));
System.out.println("LocalDate ofYearDay: " + LocalDate.ofYearDay(2020, 35));
System.out.println("-----------------");
//获取当前日期对象到1970-01-01的天数
System.out.println("localDate1 EpochDay: " + localDate1.toEpochDay());
System.out.println("-----------------");
//获取当前日期对象到指定日期的间隔
System.out.println("localDate1 until: " + localDate1.until(LocalDate.now()));
System.out.println("localDate1 until now YEARS: " + localDate1.until(LocalDate.now(), ChronoUnit.YEARS));
System.out.println("localDate1 until now MONTHS: " + localDate1.until(LocalDate.now(), ChronoUnit.MONTHS));
System.out.println("localDate1 until now DAYS: " + localDate1.until(LocalDate.now(), ChronoUnit.DAYS));
System.out.println("-----------------");
//调整日期
System.out.println("localDate1 with now: " + localDate1.with(LocalDate.now()));
System.out.println("localDate1 with Month: " + localDate1.with(Month.DECEMBER));
System.out.println("localDate1 with TemporalAdjusters: " + localDate1.with(TemporalAdjusters.firstDayOfMonth()));
System.out.println("localDate1 withDayOfMonth: " + localDate1.withDayOfMonth(2));
System.out.println("localDate1 withDayOfYear: " + localDate1.withDayOfYear(2));
System.out.println("localDate1 withMonth: " + localDate1.withMonth(12));
System.out.println("localDate1 withYear: " + localDate1.withYear(2022));
运行结果:
MAX: +999999999-12-31
-----------------
MIN: -999999999-01-01
-----------------
now: 2020-09-16
-----------------
localDate1: 2020-09-10
localDate2: 2020-09-15
-----------------
localDate2 adjustInto localDate1: 2020-09-10
localDate2: 2020-09-15
-----------------
localDate1 Start: 2020-09-10T00:00
localDate1 Start: 2020-09-10T00:00Z[Africa/Accra]
-----------------
localDate1 LocalDateTime: 2020-09-10T21:00:06.710
localDate1 LocalDateTime: 2020-09-10T12:10
localDate1 LocalDateTime: 2020-09-10T12:10:30
-----------------
localDate1 format yyyy: 2020
localDate1 format yyyy-MM-dd: 2020-09-10
-----------------
from LocalDateTime now: 2020-09-16
from LocalDate now: 2020-09-16
from ZonedDateTime now: 2020-09-16
-----------------
localDate1 get int YEAR: 2020
localDate1 get int MONTH_OF_YEAR: 9
localDate1 get int DAY_OF_YEAR: 254
localDate1 get int DAY_OF_WEEK: 4
localDate1 get long YEAR: 2020
localDate1 get long MONTH_OF_YEAR: 9
localDate1 get long DAY_OF_YEAR: 254
localDate1 get long DAY_OF_WEEK: 4
-----------------
localDate1 IsoChronology: ISO
-----------------
localDate1 DayOfMonth: 10
localDate1 DayOfWeek: THURSDAY
localDate1 DayOfYear: 254
localDate1 Month: SEPTEMBER
localDate1 MonthValue: 9
localDate1 lengthOfMonth: 30
localDate1 Year: 2020
localDate1 lengthOfYear: 366
-----------------
localDate1 isLeapYear: true
localDate1 isAfter localDate2: false
localDate1 isBefore localDate2: true
localDate1 isEqual localDate2: false
-----------------
localDate1 Era: CE
-----------------
localDate1 isSupported DAY_OF_WEEK: true
localDate1 isSupported MINUTE_OF_DAY: false
localDate1 isSupported DECADES: true
-----------------
localDate1 minus weeks: 2020-08-27
localDate1 minus DAYS: 2020-09-09
localDate1 minus weeks: 2020-09-24
localDate1 minus DAYS: 2020-09-11
localDate1 minus Days: 2020-09-09
localDate1 minus weeks: 2020-09-03
localDate1 minus Months: 2020-08-10
localDate1 minus Years: 2019-09-10
localDate1 plus Days: 2020-09-11
localDate1 plus weeks: 2020-09-17
localDate1 plus Months: 2020-10-10
localDate1 plus Years: 2021-09-10
-----------------
LocalDate of: 2020-09-16
LocalDate of: 2020-04-15
LocalDate ofEpochDay: 1970-01-16
LocalDate ofYearDay: 2020-02-04
-----------------
localDate1 EpochDay: 18515
-----------------
localDate1 until: P6D
localDate1 until now YEARS: 0
localDate1 until now MONTHS: 0
localDate1 until now DAYS: 6
-----------------
localDate1 with now: 2020-09-16
localDate1 with Month: 2020-12-10
localDate1 with TemporalAdjusters: 2020-09-01
localDate1 withDayOfMonth: 2020-09-02
localDate1 withDayOfYear: 2020-01-02
localDate1 withMonth: 2020-12-10
localDate1 withYear: 2022-09-10