Java对时间的处理

在这里插入图片描述
在这里插入图片描述
Date用的最多这里主要介绍该类。java.util 包提供了 Date 类来封装当前的日期和时间。 Date 类提供两个构造函数来实例化 Date 对象。

package com.company;
import java.util.Date;

public class Main {

    public static void main(String[] args) {
	// write your code here

        System.out.println(new Date());
        Date date = new Date();

        System.out.println("打印对象"+date);
        System.out.println("打印对象类型"+date.getClass());
        System.out.println("对象的字符串打印"+date.toString());
        System.out.println("对象的字符串打印"+date.toString().getClass());

    }
}

在这里插入图片描述

由输出可以看出Date的其toString方法输出结果是一样的,但是一个是bean对象可以操作,一个是字符串用于赋值。但是这种日期格式并不是我们需要的,该如何转化为所需的格式呢?

DateFormat对象是对日期及时间的格式化和解析工具库,SimpleDateFormat是对Date的解析库的拓展

package com.company;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Main {

    public static void main(String[] args) {
	// write your code here

        System.out.println(new Date());
        Date date = new Date();


        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");

        System.out.println("当前时间为: " + ft.format(date));
        
    }
}

SimpleDateFormat的参数可以自定义yyyy-MM-dd hh:mm:ss就是定义的输出参数。

在这里插入图片描述

在这里插入图片描述

一般这个时间字符2023-01-12 01:50:17就满足使用了,且是一个字符串类型。

Java 1.8 引入了全新的日期时间库java.timeLocalDateLocalTimeLocalDateTime,顾名思义,其意思就是本地日期、本地时间 和 本地日期时间。

LocalDate 只包含日期,例如:“2022-12-03”,而 LocalTime 则只包含时间其精确到纳秒值,例如:“12:14:23.267”。相对的 LocalDateTime其实就是LocalDate 和 LocalTime 的结合体,其包含了日期和时间。

java.time库为我们提供了创建这些日期时间的工厂方法,主要分为四类:

  • now:根据当前的日期时间来生成,同时我们也可以指定相应的时钟[Clock]或时区ID[ZoneId],否则就按照本地的时钟或时区生成。
  • parse:通过指定的日期时间的字符串生成,同时我们也可以指定字符串的格式
  • of:通过指定生成时间日期的详细信息生成
  • from:通过其它日期时间对象来生成当前类型的时间对象。

在这里插入图片描述

package com.company;

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

public class DataTestOne {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);

        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
    }
}

在这里插入图片描述

也可以通过DateTimeFormatter的方法变换为标准格式,或者获取单独的片段组装为所需的时间格式:

package com.company;

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

public class DataTestOne {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")));
    }
}

在这里插入图片描述

在这里插入图片描述

2023-01-12 02:24:12的格式和数据的Date类型是一样的,可以直接用String类型接收。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xvwen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值