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天,请努力吧

三、日期转换

1.1 代码

public static void main(String[] args) {
        dateToLocalDate();
        dateToLocalDateTime();
        localDateToDate();
        localDateTimeToDate();
    }

    public static void dateToLocalDate() {
        Date date = new Date(); // 获取当前日期和时间
        ZoneId zoneId = ZoneId.systemDefault(); // 获取系统默认时区
        ZonedDateTime zonedDateTime = date.toInstant().atZone(zoneId); // 将Date转换为ZonedDateTime
        LocalDate localDate = zonedDateTime.toLocalDate(); // 提取LocalDate
        System.out.println(localDate);
    }
    public static void dateToLocalDateTime() {
        Date date = new Date(); // 获取当前日期和时间
        ZoneId zoneId = ZoneId.systemDefault(); // 获取系统默认时区
        ZonedDateTime zonedDateTime = date.toInstant().atZone(zoneId); // 将Date转换为ZonedDateTime
        LocalDateTime localDateTime = zonedDateTime.toLocalDateTime(); // 提取LocalDateTime
        System.out.println(localDateTime);
    }
    public static void localDateToDate() {
        LocalDate localDate = LocalDate.now(); // 获取当前日期
        ZoneId zoneId = ZoneId.systemDefault(); // 获取系统默认时区
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId); // 将LocalDate与午夜时间结合
        Date date = Date.from(zonedDateTime.toInstant()); // 转换为Date
        System.out.println(date);
    }
    public static void localDateTimeToDate() {
        LocalDateTime localDateTime = LocalDateTime.now(); // 获取当前日期
        ZoneId zoneId = ZoneId.systemDefault(); // 获取系统默认时区
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId); // 将LocalDateTime与午夜时间结合
        Date date = Date.from(zonedDateTime.toInstant()); // 转换为Date
        System.out.println(date);
    }

1.2 执行结果

2024-04-19
2024-04-19T22:04:17.790
Fri Apr 19 00:00:00 CST 2024
Fri Apr 19 22:04:17 CST 2024

1.3 ZonedDateTime

可以像LocalDate一样方便地操作日期。

Date.from(ZonedDateTime.now().plusDays(7).withHour(10).withMinute(0).withSecond(0).toInstant())

总结

回到顶部

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值