java8日期类

文章目录

  • 前言
  • 一、代码部分
  • 二、运行结果
  • 总结


前言

本章节学习jdk8的日期类型。


一、代码部分

package test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;

/**
 * Create by zjg on 2023/7/16
 */
public class LocalDateTest {
    public static void main(String[] args) {
        //todo get
        LocalDate localDate=LocalDate.now();
        LocalTime localTime=LocalTime.now();
        LocalDateTime localDateTime=LocalDateTime.now();
        System.out.println("localDate:"+localDate);
        System.out.println("localTime:"+localTime);
        System.out.println("localDateTime:"+localDateTime.toString().replace("T"," "));
        //todo parse
        System.out.println();
        localDate=LocalDate.parse("2023-07-20");
        localTime=LocalTime.parse("22:54:42.349");
        localDateTime=LocalDateTime.parse("2023-07-20T22:54:42.349");
        System.out.println("localDate:"+localDate);
        System.out.println("localTime:"+localTime);
        System.out.println("localDateTime:"+localDateTime.toString().replace("T"," "));
        localDateTime=LocalDateTime.parse("2023-07-21 22:54:42",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("自定义格式解析:");
        System.out.println("localDateTime:"+localDateTime.toString().replace("T"," "));
        //todo add reduce
        localDateTime=localDateTime.plusYears(1);
        localDateTime=localDateTime.plusMonths(1);
        localDateTime=localDateTime.plusDays(1);
        System.out.println("localDateTime:"+localDateTime.toString().replace("T"," "));
        localDateTime=localDateTime.minusYears(1);
        localDateTime=localDateTime.minusMonths(1);
        localDateTime=localDateTime.minusDays(1);
        System.out.println("localDateTime:"+localDateTime.toString().replace("T"," "));
        //todo compare
        LocalDate localDate1=LocalDate.parse("2022-05-20");
        LocalDate localDate2=LocalDate.parse("2023-07-23");
        System.out.println(localDate1+" isBefore "+localDate2+":"+localDate1.isBefore(localDate2));
        System.out.println(localDate1+" isAfter "+localDate2+":"+localDate1.isAfter(localDate2));
        System.out.println(localDate1+" isEqual "+localDate2+":"+localDate1.isEqual(localDate2));
        System.out.println(localDate1+" compareTo "+localDate2+":"+localDate1.compareTo(localDate2));//正数,0,负数
        Period period = Period.between(localDate1,localDate2);
        System.out.println(String.format("years:%d,months:%d,days:%d",period.getYears(),period.getMonths(),period.getDays()));
        localDate=LocalDate.now();
        //todo LeapYear
        System.out.println(localDate.getYear()+"年"+(LocalDate.now().isLeapYear()==true?",是闰年":",不是闰年"));
        //todo new year
        LocalDate startLocaldate=LocalDate.of(localDate.getYear(),1,1);
        LocalDate endLocaldate=LocalDate.of(localDate.plusYears(1).getYear(),1,1);
        System.out.println("今天是"+localDate.getYear()+"的第"+((localDate.toEpochDay()-startLocaldate.toEpochDay())+1)+"天");
        System.out.println("距离"+endLocaldate.getYear()+"还剩下"+((endLocaldate.toEpochDay()-localDate.toEpochDay())-1)+"天,请努力吧");
    }
}

二、运行结果

localDate:2023-07-16
localTime:18:03:32.984
localDateTime:2023-07-16 18:03:32.984
localDate:2023-07-20
localTime:22:54:42.349
localDateTime:2023-07-20 22:54:42.349
自定义格式解析:
localDateTime:2023-07-21 22:54:42
localDateTime:2024-08-22 22:54:42
localDateTime:2023-07-21 22:54:42
2022-05-20 isBefore 2023-07-23:true
2022-05-20 isAfter 2023-07-23:false
2022-05-20 isEqual 2023-07-23:false
2022-05-20 compareTo 2023-07-23:-1
years:1,months:2,days:3
2023年,不是闰年
今天是2023的第197天
距离2024还剩下168天,请努力吧


总结

回到顶部

你可能感兴趣的:(java,java,开发语言)