一天一个Java知识点----JDK8的时间API(LocalDate、LocalTime、LocalDateTime以及DateTimeFormatter的使用)

JDK8开始新增的时间API

  1. 设计更合理,功能丰富,使用更方便。
  1. 都是不可变对象,修改后会返回新的时间对象,不会丢失最开始的时间。
  1. 线程安全。
  1. 能精确到毫秒、纳秒。
  • Tip:
  • 1秒=1000毫秒;
  • 1毫秒=1000微秒;
  • 1微秒=1000纳秒;

LocalDate、LocalTime、LocalDateTime

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

获取对象的方案:

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

Java
public static void main(String[] args) {
    // 1.now():根据当前时间创建LocalDate对象。
    LocalDate now = LocalDate.now();
    System.out.println("now = " + now);// now = 2025-04-08

    // 2.of(int year, int month, int dayOfMonth):根据指定的年月日创建LocalDate对象。
    LocalDate of = LocalDate.of(2002, 6, 26);
    System.out.println("of = " + of);// of = 2002-06-26

    // 3.直接修改某个信息:withYear、withMonth、withDayOfMonth、withDayOfYear
    LocalDate ld = now.withYear(2000).withMonth(11).withDayOfMonth(12);
    System.out.println("ld = " + ld);// ld = 2000-11-12

    //   新时间都是不可变对象,改时间是返回新的时间,原来的时间对象没有变化
    System.out.println("now = " + now);// now = 2025-04-08

    // 4.把某个信息加多少:plusYears、plusMonths、plusDays、plusWeeks
    LocalDate ld1 = now.plusYears(2).plusMonths(-3).plusDays(4);
    System.out.println("ld1 = " + ld1);// ld1 = 2027-01-12

    // 5.把某个信息减多少:minusYears、minusMonths、minusDays、minusWeeks
    LocalDate ld2 = now.minusYears(2).minusMonths(-3).minusDays(4);
    System.out.println("ld2 = " + ld2);// ld2 = 2023-01-12

    // 6.获取 年 月 日
    int Year = now.getYear();
    int Month = now.getMonthValue();
    int Day = now.getDayOfMonth();
    System.out.println("Year = " + Year);// Year = 2025
    System.out.println("Month = " + Month);// Month = 4
    System.out.println("Day = " + Day);// Day = 8

    // 7.判断2个日期对象,是否相等,在前还是在后:equals、isBefore、isAfter
    System.out.println("now.equals(of) = " + now.equals(of));// now.equals(of) = false
    System.out.println("now.isBefore(of) = " + now.isBefore(of));// now.isBefore(of) = false
    System.out.println("now.isAfter(of) = " + now.isAfter(of));// now.isAfter(of) = true
}

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

Java
public static void main(String[] args) {
    // 1.now():根据当前时间创建LocalTime对象。
    LocalTime now = LocalTime.now();
    System.out.println("now = " + now);// now = 22:24:17.988174900

    // 2.of(int Hour, int Minutes, int Seconds):根据指定的年月日创建LocalTime对象。
    LocalTime of = LocalTime.of(20, 21, 57);
    System.out.println("of = " + of);// of = 20:21:57

    // 3.直接修改某个信息:withHour、withMinute、withSecond、withNano
    LocalTime ld = now.withHour(10).withMinute(11).withSecond(12).withNano(174374322);

    //   新时间都是不可变对象,改时间是返回新的时间,原来的时间对象没有变化
    System.out.println("now" + now);// now = 22:24:17.988174900

    // 4.把某个信息加多少:plusHours、plusMinutes、plusSeconds、plusNanos
    LocalTime ld1 = now.plusHours(2).plusMinutes(-3).plusSeconds(4);
    System.out.println("ld1 = " + ld1);// ld1 = 00:21:17.988174900
    // 5.把某个信息减多少:minusHours、minusMinutes、minusSeconds、minusNanos
    LocalTime ld2 = now.minusHours(2).minusMinutes(-3).minusSeconds(4);
    System.out.println("ld2 = " + ld2);//ld2 = 20:38:49.415526500

    // 6.获取 时 分  秒
    int Hours = now.getHour();
    int Minutes = now.getMinute();
    int Seconds = now.getSecond();
    System.out.println("Hours = " + Hours);// Hours = 22
    System.out.println("Minutes = " + Minutes);//37
    System.out.println("Seconds = " + Seconds);//58

    // 7.判断2个时间对象,是否相等,在前还是在后:equals、isBefore、isAfter
    System.out.println("now.equals(of) = " + now.equals(of));// now.equals(of) = false
    System.out.println("now.isBefore(of) = " + now.isBefore(of));// now.isBefore(of) = false
    System.out.println("now.isAfter(of) = " + now.isAfter(of));// now.isAfter(of) = true
}

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

Java
// 1.now(): 根据当前时间创建LocalDateTime对象
LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now.toString());// 2024-05-13T16:44:29.539026300

// 2.of(int year, int month, int dayOfMonth, int hour, int minute, int second):
// 根据指定的年月日时分秒创建LocalDateTime对象
LocalDateTime of = LocalDateTime.of(2002, 6, 26, 12, 12, 12);
System.out.println("of = " + of);// of = 2002-06-26T12:12:12

// 3.直接修改某个信息: withYear、withMonth、withDayOfMonth、withDayOfYear、withHour、withMinute、withSecond、withNano
LocalDateTime ld = now.withYear(2002).withMonth(6).withDayOfMonth(26).withHour(12).withMinute(12).withSecond(12);
System.out.println("ld = " + ld);// ld = 2002-06-26T12:12:12

// 4.把某个信息加多少: plusYears、plusMonths、plusDays、plusWeeks、plusHours、plusMinutes、plusSeconds、plusNanos
LocalDateTime ld1 = now.plusYears(2).plusMonths(-3).plusDays(4);
System.out.println("ld1 = " + ld1);// ld1 = 2027-01-12T16:44:29.539026300

// 5.把某个信息减多少: minusYears、minusMonths、minusDays、minusWeeks、minusHours、minusMinutes、minusSeconds、minusNanos
LocalDateTime ld2 = now.minusYears(2).minusMonths(-3).minusDays(4);
System.out.println("ld2 = " + ld2);// ld2 = 2023-01-12T16:44:29.539026300

// 6.获取 年 月 日 时 分 秒 纳秒
int year = now.getYear();
int month = now.getMonthValue();
int dayOfMonth = now.getDayOfMonth();
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
System.out.println("year = " + year);
System.out.println("month = " + month);
System.out.println("dayOfMonth = " + dayOfMonth);
System.out.println("hour = " + hour);
System.out.println("minute = " + minute);
System.out.println("second = " + second);

// 7.判断2个日期对象,是否相等,在前还是在后: equals isBefore isAfter
System.out.println("now.equals(of) = " + now.equals(of));// false
System.out.println("now.isBefore(of) = " + now.isBefore(of));// false
System.out.println("now.isAfter(of) = " + now.isAfter(of));// true

LocalDateTime的转换成LocalDate、LocalTime

Java
// 8.把LocalDateTime转换成LocalDate
LocalDate localDate = now.toLocalDate();
System.out.println("localDate = " + localDate);// localDate = 2024-05-13

// 9.把LocalDateTime转换成LocalTime
LocalTime localTime = now.toLocalTime();
System.out.println("localTime = " + localTime);// localTime = 16:44:29.539026300

DateTimeFormatter[线程安全]

  • DateTimeFormatter:格式化器,用于时间的格式化、解析

DateTimeFormatter:

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

Java
// 1.获取指定格式化器
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");

// 2.获取当前时间的LocalDateTime
LocalDateTime now = LocalDateTime.now();

// 格式化(建议)
// 3.使用LocalDateTime的format对时间进行格式化
String format = now.format(dtf);
System.out.println("格式化 format = " + format); // 2024年05月13日 17时47分55秒

// 4.使用LocalDateTime的parse对时间进行解析(建议)
LocalDateTime ldt = LocalDateTime.parse("2018年11月22日 13时14分15秒", dtf);
System.out.println("ldt = " + ldt); // 2018-11-22T13:14:15

System.out.println("----------------");
// 格式化(不建议)
String format1 = dtf.format(now);
System.out.println("format1 = " + format1); // 2024年05月13日 17时50分34秒
// 解析(不建议)
TemporalAccessor parse = dtf.parse("2018年11月22日 13时14分15秒");
System.out.println("parse = " + parse); // {},ISO resolved to 2018-11-22T13:14:15
LocalDateTime localDateTime = LocalDateTime.from(parse);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值