文章目录
黑马学习笔记
使用新增的
为什么要使用新增的API

新增了哪些?

Local
常用方法

代码
            
            
              java
              
              
            
          
          package NewTime;
import java.time.LocalDate;
/**
 * @Author: ggdpzhk
 * @CreateTime: 2024-08-27
 * LocalDate对象是不可变的,每次修改 或者加减  都要新建一个对象
 */
public class LovalTest {
    public static void main(String[] args) {
        //1. 获取本地的日期对象
        LocalDate now = LocalDate.now();
        System.out.println(now);
        //2. 获取日期对象中的信息
        int year = now.getYear();
        System.out.println(year);
        int month = now.getMonthValue();//1-12
        System.out.println(month);
        int date = now.getDayOfMonth();
        System.out.println(date);
        int dayOfWeek = now.getDayOfWeek().getValue();//1-7 星期几
        System.out.println(dayOfWeek);
        System.out.println("~~~~~~~~~~~");
        //3. 直接修改某个信息
        LocalDate ld1 = now.withYear(2000);
        System.out.println(ld1);
        System.out.println(now);
        //4. 把某个信息加多少
        LocalDate ld2 = now.plusDays(10);
        System.out.println(ld2);
        LocalDate ld3 = now.plusMonths(2);
        System.out.println(ld3);
        LocalDate ld4 = ld2.plusYears(2);
        System.out.println(ld4);
        System.out.println("~~~~~~~~~~~~~~~");
        //6. 获取指定日期的LocalDate对象
        LocalDate ld5 = LocalDate.of(2024, 9, 1);
        System.out.println(ld5);
        //5. 把某个信息减多少
        LocalDate ld6 = now.minusDays(10);
        System.out.println(ld6);
        //7. 判断2个日期对象是否相等,在前还是在后
        System.out.println(ld5.isAfter(now));
    }
}
        
一样的用法



