Java学习之路(三十六)| 日期类LocalDate(第三代)

各自努力,最高处见!加油!

日期类LocalDate类(第三代)


JDK8后加入的类


import java.time.LocalDate;
import java.time.LocalDateTime;

public class LocalDate_ {
    public static void main(String[] args) {
        LocalDateTime localDateTime=LocalDateTime.now();
        System.out.println(localDateTime);//2021-09-14T00:02:02.637340300
        System.out.println(localDateTime.getYear()+"-"+localDateTime.getMonth()+"-"+localDateTime.getDayOfMonth());
        //2021-SEPTEMBER-14

        System.out.println(localDateTime.getYear()+"-"+localDateTime.getMonthValue()+"-"+localDateTime.getDayOfMonth());
        //2021-9-14
    }
}

格式化方法

使用DateTimeFormatter对象格式化日期。

        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分SS秒");
        String format=dateTimeFormatter.format(localDateTime);
        System.out.println(format);

		LocalDateTime ldt=localDateTime.plusDays(569);
        System.out.println(ldt.getYear()+"-"+ldt.getMonth()+"-"+ldt.getDayOfMonth());//569天后

Instant时间戳

提供了一系列和Date类转换的方式
Instant----->Date:

Date date=Date.from(instant);

Date----->Instant:

Instant instant=date.toInstant();
import java.time.Instant;
import java.util.Date;

public class Instant_ {
    public static void main(String[] args) {
        Instant now=Instant.now();
        System.out.println(now);

        Date date= Date.from(now);//Instant的from方法将Instant转成Date对象
        System.out.println(date);

        Instant instant=date.toInstant();//Date的toInstant方法将date转换成Instant对象
        System.out.println(instant);
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值