【Java】LocalDate、LocalTime、LocalDateTime方法测试

本文介绍了一个简单的Java程序,用于测试Java 8中的日期和时间API。程序演示了如何获取当前的日期、时间和日期时间,并展示了如何使用DateTimeFormatter将日期时间格式化为特定的字符串格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Code:

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

public class Test {
    public static void main(String[] args) throws InterruptedException {
        //LocalDate、LocalTime、LocalDateTime方法测试
        LocalDate now = LocalDate.now();
        LocalTime now1 = LocalTime.now();
        LocalDateTime now2 = LocalDateTime.now();

        System.out.println(now);
        System.out.println(now1);
        System.out.println(now2);
        System.out.println(now2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));


        //LocalDateTime now1 = LocalDateTime.now();
        //Thread.sleep(1000 * 3);
        //LocalDateTime now2 = LocalDateTime.now();
        //Duration duration = Duration.between(now1, now2);
        ////计算相差多少秒
        //System.out.println(duration.toMillis() / 1000);
    }
}

Result:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值