java8新特性——Date Time API

java8新特性之-----

Date Time API:Date Time API 强化了Java对日期和时间的处理,它更容易使用、更安全,同时提供了更多的功能,如时区支持、日期时间计算等。

以前,Java的日期时间处理非常繁琐,而新的API使日期时间操作变得更加直观和易用。例如,你可以使用LocalDate来表示日期,LocalTime来表示时间,而不再需要混合使用Date和Calendar。

代码演示:

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

public class dateTimeApi {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        System.out.println("Formatted Date: " + localDate.format(dateFormatter));
        System.out.println("Formatted Time: " + localTime.format(timeFormatter));
        System.out.println("Formatted Date Time: " + localDateTime.format(dateTimeFormatter));
    }
}

观察输出:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值