日期相关类 (Java)

日期相关类

方法
System 系统类currentTimeMillis(): 当前时间
Date 日期类new Date() :当前时间
new Date(指定时间):指定时间
getTime(): 获取时间
setTime(指定时间):设置指定时间
SimpleDateFormat 格式器new SimpleDateFormat() 构造方法 (模板)
format() : 格式化为字符串
parse():转成日期
System 系统类

​ 获取当前时间 (1970年1 月 1 日 00:00:00 GMT 到现在的毫秒) 是long类型

System.out.println(System.currentTimeMillis());
// 运行结果:
1603869696005
Date 类 构造方法 (java.util.Date)

​ Date date1 = new Date(); // 无参构造默认得到的是当前时间的毫秒数
Date date2 = new Date(long类型整数); // 有参构造默认得到的是输入的毫秒数

Date date = new Date();  
System.out.println(date);   // date重写了的toString方法 打印出了时间

Date date1 = new Date(0);  // int 的 0 自动转化成 long 的0
System.out.println(date1); // date重写了的toString方法 打印出了时间
// 运行结果:
Wed Oct 28 15:40:50 CST 2020
Thu Jan 01 08:00:00 CST 1970
Date类 方法

getTime() 返回对象的毫秒数

Date date2 = new Date(0);
long ti = date2.getTime();
System.out.println(ti);
// 运行结果:
0
SimpleDateFormat格式器
// 1、将时间以我们喜欢|习惯的方式进行展示
// 格式化工具类, 指定格式   方法1:new SimpleDateFormat()

SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  // 格式可以根据需要改变
//  将时间对象 按照指定的格式显示
String date1Str = sf.format(date1);  // 方法2: format() 格式化为字符串
System.out.println(date1Str);

SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");   // 格式可以根据需要改变
//  将时间对象 按照指定的格式显示
String date1Str2 = sf2.format(date1);
System.out.println(date1Str2);


// 2、将指定的时间点进行存储
// 1999/9/9 9:9:9
String str = "1999/9/9 9:9:9";
// 将指定好格式的字符串,按照格式转换成日期
Date date2 = sf.parse(str);   // 方法3:把模板对应的格式转化为Date类型
System.out.println(date2);


// 运行结果:
2020/10/28 15:58:41
2020-10-28 15-58-41
Thu Sep 09 09:09:09 CST 1999

在这里插入图片描述

JDK8的日期类 (常用) (java.time)

java8提供了更为简洁的,更明了的常用日期类

方法
年 Yearnow()
of()
月 Monthof()
valueOf
JANUARY:一月 等等 (枚举类型)

年 Year Year是 final 修饰的类

月 Month Month是枚举类型

// 年
Year year = Year.now();    // Year是 final 修饰的类
System.out.println(year);

year = Year.of(2021);
System.out.println(year);

// 月

Month month = Month.of(1);    //  Month是枚举类型
System.out.println(month);

month = Month.valueOf("JANUARY");
System.out.println(month);

System.out.println(Month.JANUARY);


// 运行结果:
2020
2021
JANUARY
JANUARY
JANUARY
方法
LocalDate 日期(年月日)now():不包含时分秒的本时区的当前时间
of(int year,int month, int dayOfMonth):构建本时区的时间
of(int year,Month month,int dayOfMonth)
LocalTime 时间(时分秒)now()
of(int hour, int minute )
of(int hour ,int minute, int second)
LocalDateTime 日期时间now( )
of(LocalDate date , LocalTime time)
of(int year, int month, int dayOfMonth, int hour, int minute)
of(int year, Month month, int dayOfMonth, int hour, int minute)
of(int year, int month, int dayOfMonth, int hour, int minute, int second)
of(int year, Month month, int dayOfMonth, int hour, int minute, int second)
format(模板对象) : LocalDateTime 格式化为 字符串
parse(String,模板对象 ): 转成日期
DateTimeFormatterofPattern(“yyyy年MM月dd日 HH:mm:ss”)

LocalDate:代表日期,可以通过年、月、日构造,并且年/月/日的值都是符合日常使用习惯的

​ LocalDate of(int year,int month, int dayOfMonth)
LocalDate of(int year,Month month,int dayOfMonth)

LocalTime:代表时间,可以通过时、分、秒来构造

​ LocalTime now()
LocalTime of(int hour, int minute )
LocalTime of(int hour ,int minute, int second)

LocalDateTime:表示日期和时间,可以通过年、月、日、时、分、秒来构造,也可以通过一个LocalDate和LocalTime来构造。可以调用with、plus、minus方法来修改LocalDateTime对象,但是修改后原对象的值不变,会返回一个新的对象。

​ LocalDateTime now( )

​ LocalDateTime of(LocalDate date , LocalTime time)

​ LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)

​ LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute)

​ LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)

​ LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second)

// 日期
LocalDate date = LocalDate.of(2020,2,29);
System.out.println(date);

date = LocalDate.now();
System.out.println(date);

// 时间
LocalTime time1 = LocalTime.now();
System.out.println(time1);


// 日期和时间
LocalDateTime time = LocalDateTime.now();
System.out.println(time);

// 运行结果:
2020-02-29
2020-10-28
17:31:42.340
2020-10-28T17:31:42.340

格式化

​ DateTimeFormatter 类,它的使用也非常简单,调用静态方法ofPattern 生成指定匹配格式的实例

​ 调用LocalDateTime实例的 format 方法可以将 TemporalAccessor 对象转换成指定格式的日期时间字符串,实际上这个TemporalAccessor 是个接口,前面介绍的LocalTime/LocalDate/LocalDateTime 都间接实现了这个接口; (返回的是 String)

​ 调用LocalDateTime实例的 parse 方法可以将时间日期字符串转换为 TemporalAccessor 对象,进而得到对应的LocalTime/LocalDate/LocalDateTime 对象 (返回的是 LocalDateTime)

// 格式器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

// LocalDateTime 格式化日期 转成 字符串
LocalDateTime time5 = LocalDateTime.now();
String dateStr = time5.format(formatter);
System.out.println(dateStr);

//LocalDateTime 指定字符串 转成 模板的日期
String s= "2020年10月12日 15:00:26";
LocalDateTime t = LocalDateTime.parse(s, formatter);
System.out.println(t);

// 运行结果:
2020102817:36:05
2020-10-12T15:00:26

时间间隔

方法
Period 时期between():间隔 年月日(基于日期)
Duration 持续期间Duration.ofDays(天数):天
Duration.ofHours(小时数):时
Duration.ofMinutes(分钟数):分
Duration.ofSeconds(秒数):秒
Duration.ofMillis() 毫秒
Duration.between(inst1, inst2).toDays( ) 相差的天数
Duration.between(inst1, inst2).toHours ( ) 相差的小时
Duration.between(inst1, inst2).toMinutes ( ) 相差的分钟
Duration.between(inst1, inst2).getSeconds ( ) 相差的秒数
Duration.between(inst1, inst2).toMillis()相差的毫秒数
ChronoUnit 期量单位YEARS、MONTHS、DAYS、HOURS、MINUTES、SECONDS
getDuration() :获取Duration
between():间隔
Instant 瞬间now():现在
plus():添加
minus():相减
// Period
// 选择一个日期
LocalDate birthDate = LocalDate.of(1999, 3, 15);

// 获取当前日期
LocalDate today = LocalDate.now();

// Period 存储的是两个日期之差
Period num = Period.between(birthDate, today);
System.out.println("年月日:"+num);
System.out.println("-----");
System.out.println("年"+num.getYears());
System.out.println("月"+num.getMonths());
System.out.println("日"+num.getDays());

// 运行结果:
年月日:P21Y7M13D
-----21713
// ChronoUnit
// 期量单位
LocalDate startDate = LocalDate.of(1993, 8, 19);
LocalDate endDate = LocalDate.of(1993, 9,19);
long year = ChronoUnit.YEARS.between(startDate, endDate);
System.out.println(year);

long month =ChronoUnit.MONTHS.between(startDate, endDate);
System.out.println(month);

long day =ChronoUnit.DAYS.between(startDate, endDate);
System.out.println(day);
// 运行结果:
0
1
31
// Instant
//瞬间
Instant inst1 = Instant.now();
Instant inst2 = inst1.plus(Duration.ofSeconds(1));

System.out.println("毫秒相隔 : " + Duration.between(inst1, inst2).toMillis());
System.out.println("秒相隔 : " + Duration.between(inst1, inst2).getSeconds());
// 运行结果:
毫秒相隔 : 1000
秒相隔 : 1
``


```java
// Instant
//瞬间
Instant inst1 = Instant.now();
Instant inst2 = inst1.plus(Duration.ofSeconds(1));

System.out.println("毫秒相隔 : " + Duration.between(inst1, inst2).toMillis());
System.out.println("秒相隔 : " + Duration.between(inst1, inst2).getSeconds());
// 运行结果:
毫秒相隔 : 1000
秒相隔 : 1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值