最近在准备入职材料,一放松就不想学习,果然堕落永远是最轻松的。

今日总结:

Java学习:

常用API:

LocalDate:代表本地日期(年、月、日、星期)

LocalTime:代表本地时间(时、分、秒、纳秒)

LocalDateTime:代表本地日期、时间(年、月、日、星期、时、分、秒、纳秒)

它们获取对象的方案

方法名

示例

public static Xxxx now():获取系统当前时间对应的该对象

LocalDate ld = LocalDate.now();

LocalTime It = LocalTime.now():

LocalDateTime ldt = LocalDateTime.now();

public static Xxxx of(...):获取指定时间的对象

LocalDate ld = LocalDate.of(2099,11,11);

LocalTime lt = LocalTime.of(9,8,59);

LocalDateTime ldt = LocalDateTime.of(2025,11,16,14,30,01);

LocalDate的常用API (都是处理年、月、日、星期相关的)。

方法名

说明

public int geYear()

获取年

public int getMonthValue()

获取月份(1-12)

public int getDayOfMonth()

获取日

public int getDayOfYear()

获取当前是一年中的第几天

public DayOfWeek getDayOfWeek()

获取星期几: ld.getDayOfWeek(). getValue()

方法名

说明

withYear、withMonth、 withDayOfMonth、withDayOfYear

直接修改某个信息,返回新日期对象

plusYears、plusMonths、plusDays、plusWeeks

把某个信息加多少,返回新日期对象

minusYears、minusMonths、minusDays、minusWeeks

把某个信息减多少,返回新日期对象

equals isBefore isAfter

判断两个日期对象,是否相等,在前还是在后

LocalTime的常用API(都是处理时、分、秒、纳秒相关的)。

方法名

说明

public int getHour()

获取小时

public int getMinute()

获取分

public int getSecond()

获取秒

public int getNano()

获取纳秒

方法名

说明

withHour、withMinute、wi thSecond、withNano

修改时间,返回新时间对象

plusHours、plusMinutes、plusSeconds、plusNano

把某个信息加多少,返回新时间对象

minusHours、minusMinutes、minusSeconds、minusNanos

把某个信息减多少,返回新时间对象

equals isBefore isAfter

判断2个时间对象,是否相等,在前还是在后

LocalDateTime的常用API (可以处理年、月、日、星期、时、分、秒、纳秒等信息)

方法名

说明

getYear、getMonthValue、ge tDayOfMonth、getDayOfYear、getDayOfWeek、getHour、getMinute、getSecond、getNano

获取年月日、时分秒、纳秒等

withYear、withMonth、withDayOfMonth、withDayOfYear、withHour、withMinute、 withSecond、 withNano

修改某个信息,返回新日期时间对象

plusYears、plusMonths、 plusDays、plusWeeks、plusHours、plusMinutes、plusSeconds、plusNanos

把某个信息加多少,返回新日期时间对象

minusYears、minusMonths、minusDays、minusWeeks、minusHours、minusMinutes、minusSeconds、minusNanos

把某个信息减多少,返回新日期时间对象

equals isBefore isAfter

判断2个时间对象,是否相等,在前还是在后

Zoneld时区的常见方法

方法名

说明

public static Set<String> getAvailableZonelds()

获取Java中支持的所有时区

public static Zoneld systemDefault()

获取系统默认时区

public static Zoneld of(String zoneld)

获取一个指定时区

ZoneDateTime带时区时间的常见方法

方法名

说明

public static ZonedDateTime now()

获取当前时区的ZonedDateTime对象

public static ZonedDateTime now(Zoneld zone)

获取指定时区的ZonedDateTime对象

getYear、getMonthValue、getDayOfMonth、getDayOfYear、getDayOfWeek、getHour、getMinute、getSecond、getNano

获取年月日、时分秒、纳秒等

public ZonedDateTime withXxx(时间)

修改时间系列的方法

public ZonedDateTime minusXxx(时间)

减少时间系列的方法

public ZonedDateTime plusXxx(时间)

增加时间系列的方法

注:以上几种方法用于代替旧版JDK中的Calendar

Instant:时间线上的某个时刻/时间戳

●通过获取Instant的对象可以拿到此刻的时间,该时间由两部分组成:从1970-01-01 00:00:00开始走到此刻的总秒数+不够1秒的纳秒数

方法名

说明

public static Instant now()

获取当前时间的Instant对象(标准时间)

public long getEpochSecond()

获取从1970-01-01 00:00:00开始记录的秒数

public int getNano()

从时间线开始,获取从第二个开始的纳秒数

plusSeconds plusMillis plusNanos

增加时间系列的方法

minusSeconds minusMillis minusNanos

减少时间系列的方法

equals isBefore isAfter

判断系列的方法

●作用:可以用来记录代码的执行时间,或用于记录用户操作某个事件的时间点。

●传统的Date类,只能精确到毫秒,并且是可变对象;

●新增的Instant类,可以精确到纳秒,并且是不可变对象,推荐用Instant代替Date。

DateTimeFormatter:时间格式化

方法名

说明

public static DateTimeFormatter ofPattern(时间格式)

获取格式化器对象

public String format(时间对象)

格式化时间

LocalDateTime提供的格式化、解析时间的方法

方法名

说明

public String format(DateTimeFormatter formatter)

格式化时间

public static LocalDateTime parse(CharSequence text,DateT imeFormatter formatter)

解析时间

补充知识:

Period(一段时期)

●可以用于计算两个LocalDate对象相差的年数、月数、天数。

方法名

说明

public static Period between(LocalDate start,LocalDate end)

传入2个日期对象,得到Period对象

public int getYears()

计算隔几年,并返回

public int getMonths()

计算隔几个月,并返回

public int getDays()

计算隔多少天,并返回

Duration(持续时间)

●可以用于计算两个时间对象相差的天数、小时数、分数、秒数、纳秒数;支持LocalTime、LocalDateTime、Instant等时间。

方法名

说明

public static Duration between(开始时间对象1,截止时间对象2)

传入2个时间对象,得到Duration对象

public long toDays()

计算隔多少天,并返回

public long toHours()

计算隔多少小时,并返回

public long toMinutes()

计算隔多少分,并返回

public long toSeconds()

计算隔多少秒,并返回

public long toMillis()

计算隔多少毫秒,并返回

public long toNanos()

计算隔多少纳秒,并返回