Java 日期和时间接口 Date-Time API

一、JDK8之前的日期和时间的API

1.System类中的currentTimeMillis()

返回当前时间与1970年1月1日0分0秒之间以毫秒为单位的时间差,称为时间戳

2.Java.util.Date类 |---java.sql.Date类

1.两个构造器的使用

>构造器一: Date(): 创建一个对应当前时间的Date对象

>构造器二:创建指定毫秒数的Date对象

 2.两个方法的使用

>toString(): 显示当前的年、月、日、时、分、秒

> getTime(): 获取当前Date对象对应的毫秒数(时间戳)

 3.java.sql.Date对应着数据库中的日期类型的变量

>如何实例化

>如何将java.util.Date对象转换为java.sql.Date对象

 

 3.SimpleDateFormat的使用: simpleDateFormat对日期Date类的格式化和解析

 1.两个操作:

>格式化:日期 --->字符串

>解析:格式化的逆过程,字符串 ---> 日期

 

 4.Calendar日历类(抽象类)的使用

1.实例化

方式一:创建其子类 (GregorianCalendar) 的对象

方式二:调用其静态方法getInstance()

Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getClass());

 2.常用方法

getTime(): 日历类---> Date

Date date = calendar.getTime();

System.out.println(date);

setTime():Date ---> 日历类

Date date1 = new Date();

calendar.setTime(date1);

days = calendar.get(Calendar.DAY_OF_MONTH);

System.out.println(days);

 

 5.时区

时区:正式的时区划分为每隔经度 15° 划分一个时区,全球共 24 个时区,每个时区相差 1 小时。但为了行政上的方便,常将 1 个国家或 1 个省份划在一起,比如我国幅员宽广,大概横跨 5 个时区,实际上只用东八时区的标准时即北京时间为准。

java.util.Date 对象实质上存的是 1970 年 1 月 1 日 0 点( GMT)至 Date 对象所表示时刻所经过的毫秒数。也就是说不管在哪个时区 new Date,它记录的毫秒数都一样,和时区无关。但在使用上应该把它转换成当地时间,这就涉及到了时间的国际化。

java.util.Date 本身并不支持国际化,需要借助 TimeZone。

//北京时间:Wed Jan 27 14:05:29 CST 2021
Date date = new Date();

SimpleDateFormat bjSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//北京时区
bjSdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
System.out.println("毫秒数:" + date.getTime() + ", 北京时间:" + bjSdf.format(date));

//东京时区
SimpleDateFormat tokyoSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
tokyoSdf.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));  // 设置东京时区
System.out.println("毫秒数:" + date.getTime() + ", 东京时间:" + tokyoSdf.format(date));

//如果直接print会自动转成当前时区的时间
System.out.println(date);
//Wed Jan 27 14:05:29 CST 2021

 二、JDK8中的日期和时间的API

 1.出现的背景

 

这是对java.util.Date强有力的补充,解决了 Date 类的大部分痛点:

  1. 非线程安全
  2. 时区处理麻烦
  3. 各种格式化、和时间计算繁琐
  4. 设计有缺陷,Date 类同时包含日期和时间;还有一个 java.sql.Date,容易混淆。

 2.LocalDate、LocalTime、LocalDateTime 的使用:

1.LocaLDateTime相较FLocalDate、LocalTime,使用频率要高

2.类似于Calendar

//now(): 获取当的日期、时间、日期+时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();

//of(): 设置指定的年、月、日、时、分、秒。没有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of( year: 2020, month: 10, dayOfMonth: 22)
LocalDateTime localDateTime2 = LocalDateTime.parse("2020-10-22")
System.out.println(localDateTime1);

//getXxx(): 获取相关的属性
System.out.printIn(localDateTime.getDayofMonth());
System.out.printIn(localDateTime.getDayofWeek());
System.out.printIn(localDateTime.getMonth());
System.out.printIn(localDateTime.getMonthValue());
System.out.println(localDateTime.getMinute());

//体现不可变性
//withXxx(): 设置相关的属性
//设置时间为22号
LocalDate localDate1 = localDate.withDayofMonth(22);
System.out.printIn(localDate);
System.out.println(localDate1);

//设置时间为4小时
LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.printIn(localDateTime);
System.out.printIn(localDateTime2);
//不可变性
//设置时间为向后延迟3个月
LocalDateTime localDateTime3 = locaDateTime.plusMonths(3);
System.out.println(localDateTime);
System.out.println(localDateTime3);

//LocalDatetime转Java.util.date
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime endLocal= LocalDateTime.now();
Date endTime = Date.from(endLocal.atZone(zoneId).toInstant());

3.Instant类的使用

Instant: 时间线上的一个瞬时点。 这可能被用来记录应用程序中的事件时间戳。

类似于Java.Util.Date类

//now(): 获取本初子午线对应的标准时间
Instant instant = Instant .now();
System.out.println(instant);//2019-02-18T7:29:41.719Z

//添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System,out.println(offsetDateTime);//2019-02-18T15:32:50.611+08:00

//toEpochMilli(): 获取自197年1月1时分秒(UTC)开始的毫秒数---> Date类的getTime
long milli = instant.toEpochMilli();
System.out.println(milli);

//ofEpochMilLi():通过给定的毫秒数,获取Instant实例 -->Date(Long millis)
Instant instant1 = Instant.ofEpochMilli(1550475314878L);
System.out.println(instant1);

 4.DateTimeFormatter的使用:格式化或解析日期、时间

重点: 方式三:自定义的格式。如: ofPattern(“yyyy-MM-dd hh:mm:ss”)
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")
//格式化
String str4 = formatter3.format(LocalDateTime.now());
System.out.println(str4);//2919-02-18 3:52:09

5.时区 :ZonedDateTime

时区:正式的时区划分为每隔经度 15° 划分一个时区,全球共 24 个时区,每个时区相差 1 小时。但为了行政上的方便,常将 1 个国家或 1 个省份划在一起,比如我国幅员宽广,大概横跨 5 个时区,实际上只用东八时区的标准时即北京时间为准。

Java8 引入了java.time.ZonedDateTime 来表示带时区的时间。它可以看成是 LocalDateTime + ZoneId。

//当前时区时间
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("当前时区时间: " + zonedDateTime);

//东京时间
ZoneId zoneId = ZoneId.of(ZoneId.SHORT_IDS.get("JST"));
ZonedDateTime tokyoTime = zonedDateTime.withZoneSameInstant(zoneId);
System.out.println("东京时间: " + tokyoTime);

// ZonedDateTime 转 LocalDateTime
LocalDateTime localDateTime = tokyoTime.toLocalDateTime();
System.out.println("东京时间转当地时间: " + localDateTime);

//LocalDateTime 转 ZonedDateTime
ZonedDateTime localZoned = localDateTime.atZone(ZoneId.systemDefault());
System.out.println("本地时区时间: " + localZoned);

//打印结果
当前时区时间: 2021-01-27T14:43:58.735+08:00[Asia/Shanghai]
东京时间: 2021-01-27T15:43:58.735+09:00[Asia/Tokyo]
东京时间转当地时间: 2021-01-27T15:43:58.735
当地时区时间: 2021-01-27T15:53:35.618+08:00[Asia/Shanghai]

6.其他日期API

 

 

 

 

 三、与传统日期处理API的转换

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值