Java 新增日期时间类

123 阅读2分钟

 LocalDate 

 不包含具体时间的日期 

public static void main(String[] args) {
    LocalDate nowDate = LocalDate.now(); 
    System.out.println(nowDate); // 2023-04-19
    int year = nowDate.getYear();
    System.out.println(year);
    int month = nowDate.getMonthValue(); 
    System.out.println(month);
    int day = nowDate.getDayOfMonth(); 
    System.out.println(day); // 当年的第几天 
    int dayOfYear = nowDate.getDayOfYear(); 
    System.out.println(dayOfYear); // 星期 
    System.out.println(nowDate.getDayOfWeek()); 
    System.out.println(nowDate.getDayOfWeek().getValue()); // 创建某个日期 
    LocalDate ld = LocalDate.of(1989,02,27); 
    System.out.println(ld);
 } 

 LocalTime

        不包含日期的时间 。

public static void main(String[] args) { 
     LocalTime localTime = LocalTime.now();
     System.out.println(localTime);
     int hour = localTime.getHour(); 
     System.out.println(hour); 
     int minute = localTime.getMinute();
     System.out.println(minute);
     int second = localTime.getSecond();
     System.out.println(second); // 创建某个时刻 
     System.out.println(LocalTime.of(10, 20, 3));
}

 LocalDateTime 

       包含日期及时间。

 public static void main(String[] args) {
    LocalDateTime ldt = LocalDateTime.now();
    System.out.println(ldt); 
    System.out.println(ldt.getYear());
    System.out.println(ldt.getHour()); // 创建某个日期时刻
    LocalDateTime.of(2022,3,4,3,4,5,100);
 }

相关API

        LocalDateTime综合了 LocalDate 和LocalTime 里面的方法; 

        以下方法返回的是一个新的实例引用,因为是不可变的; 

         1. plusDays 、plusWeeks、plusMonths、plusYears : 向当前LocalDate对象添加几天、几周、几个月、几年;

         2. minusDays、minusWeeks、minusMonths、minusYears : 从当前LocalDate对象减去几天、几周、几月、几年;

         3. withDayOfMonth、withDayOfYear、with Month、withUear : 将月份天数、年份天数、月份、年份修改为指定的值并返回新的对象

         4. isBefore 、 isAfter: 比较两个LocalDate。

 Instant 

      代表时间戳 

     JDK8 获取时间戳特别简单,且功能丰富,Instant类由一个静态工厂方法now() 可以返回当前时间戳。 

      时间戳是包含日期和时间的,与java.util.Date很类似。

      Instant和Date这两个类可以进行转换;

public static void main(String[] args) { 
    // 创建时间戳对象
    Instant it = Instant.now(); 
    System.out.println(it); // 标准时间 
   // 获取毫秒值
   System.out.println(localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli()); 
   // 系统此时时间戳 
   System.out.println(it.atZone(ZoneId.systemDefault())); } 

 DateTimeFormatter 

        用于做时间的格式化和解析 

        在JDK8 中,引入了一个全新的日期与时间格式器 DateTimeFormatter;

        正反都能调用formart 方法; 

 public static void main(String[] args) {
     LocalDateTime localDateTime = LocalDateTime.now(); 
     System.out.println(localDateTime);
     DateTimeFormatter drf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 
     String ldtStr = localDateTime.format(drf);
     System.out.println(ldtStr); 
     String ldtStr2 = drf.format(localDateTime); 
     System.out.println(ldtStr2); 
     System.out.println(ldtStr.equals(ldtStr2)); // true 
} 

Duration

       计算两个时间的间隔 

       提供了使用基于时间的值测量时间量的方法 

       用于LocalDateTime之间的比较,也可以用于Instant之间的比较

public static void main(String[] args) {
     LocalDateTime ldt = LocalDateTime.now();
     System.out.println(ldt);
     LocalDateTime birth = LocalDateTime.of(1989,2,27,23,10,11);
     System.out.println(birth);
     Duration duration = Duration.between(birth,ldt);
     System.out.println(duration);
     System.out.println(duration.toDays()); // 相差天数
     System.out.println(duration.toHours()); // 相差小时数 
     System.out.println(duration.toMinutes()); // 相差小时数 
     System.out.println(duration.toMillis()); // 相差毫秒数

Period 

        用于计算两个日期的间隔 

       主要是Period类方法 getYear(),getMonths() 和 getDays()来计算,只能精确到年月日

       用户LocalDate之间比较 。

public static void main(String[] args) {
     LocalDate ld = LocalDate.now();
     System.out.println(ld);
     LocalDate birth = LocalDate.of(1989,2,27); 
     System.out.println(birth);
     Period pd = Period.between(birth,ld); 
     System.out.println(pd);
     System.out.print("相差"+pd.getYears()+"年"); 
     System.out.print(pd.getMonths()+"个月");
     System.out.println(pd.getDays()+"天"); 
}

ChronoUnit 类

         可用于在单个时间单位内测量一段时间,可以比较所有的时间单位;、

总结: 

         新增的API严格区分了时刻、本地日期、本地时间,并且对日期和时间进行运算更加方便。

          其次,新API的类型几乎全部是不变类型,可以放心使用不必担心被修改。