第28讲 日历时间日期相关操作类(JDK8以后)

本文介绍了JDK8及以后版本中的时间类改进,强调了时间对象的不可变性。讲解了ZoneId如何获取时区,Instant如何处理时间戳,以及ZoneDateTime和DateTimeFormatter在处理带时区时间和格式化方面的应用。还涵盖了LocalDate、LocalTime、LocalDateTime、Duration、Period和ChronoUnit等工具类的常用方法。
摘要由CSDN通过智能技术生成

JDK8以后的时间类

  • 时间日期对象是不可变的,解决了JDK7以前的问题

修改(with)、增加(plus)、减少(minus)时间,不会改变调用者,而是会产生一个新的时间对象。

1 ZoneId 时区类

导包:java.time.ZoneId

常用方法

方法说明
static Set getAvailableZoneIds()获取Java支持的所有时区
static ZoneId systemDefault()获取系统默认时区
static ZoneId of(String zoneId)获取指定时区
import java.time.ZoneId;
import java.util.Set;

public class ZoneIdDemo {
    public static void main(String[] args) {
        //获取Java支持的所有时区
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        System.out.println(zoneIds);//[Asia/Aden, America/Cuiaba, Etc/GMT+9, Etc/GMT+8, ...]
        System.out.println(zoneIds.size()); //602

        //获取系统默认时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId); //Asia/Shanghai

        //获取指定时区
        ZoneId zoneIdOf = ZoneId.of("Asia/Aden");
        System.out.println(zoneIdOf);   //Asia/Aden
    }

2 Instant 时间戳

导包:java.time.Instant

常用方法

方法说明
static Instant now()获取当前时间(世界标准时间)的Instant对象
static Instant ofXxx(long e)根据e值(秒/毫秒/纳秒)获取从基准时间开始的Instant对象
ZonedDateTime atZone(ZoneId zone)获取指定时区的ZonedDateTime对象
boolean isBefore/isAfter(Instant otherInstant)判断调用者时间是否在参数时间的前/后
Instant minusXxx(long m)减少时间系列方法
Instant plusXxx(long m)增加时间系列方法
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class InstantDemo {
    public static void main(String[] args) {
        //获取当前时间(世界标准时间)的Instant对象
        Instant now = Instant.now();
        System.out.println(now); //2022-12-06T14:44:14.116076800Z

        //根据e值(秒/毫秒/纳秒)获取从基准时间开始的Instant对象
        //秒
        Instant ofEpochSecond1 = Instant.ofEpochSecond(100L);
        System.out.println(ofEpochSecond1);  //1970-01-01T00:01:40Z
        //毫秒
        Instant ofEpochMilli = Instant.ofEpochMilli(100L);
        System.out.println(ofEpochMilli);    //1970-01-01T00:00:00.100Z
        //秒 + 纳秒
        Instant ofEpochSecond2 = Instant.ofEpochSecond(100L, 100L);
        System.out.println(ofEpochSecond2);  //1970-01-01T00:01:40.000000100Z

        //获取指定时区的ZonedDateTime对象
        //Instant对象,用来调用atZone()方法
        Instant instant = Instant.now();
        //ZoneId对象,用来作为atZone()方法的参数
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime zonedDateTime = instant.atZone(zoneId);
        System.out.println(zonedDateTime);  //2022-12-06T22:50:46.401115100+08:00[Asia/Shanghai]
    }
}

3 ZoneDateTime 带时区的时间

导包:java.time.ZonedDateTime

常用方法

方法说明
static ZoneDateTime now()获取当前时间(当前时区)的ZoneDateTime对象
static ZoneDateTime of(int y M d H m S, ZoneId z)获取指定时区指定年月日等的ZoneDateTime对象
static ZoneDateTime ofInstant(Instant i, ZoneId z)获取指定时间戳指定时区的ZoneDateTime对象
ZoneDateTime withXxx(long m)修改时间系列方法
Instant minusXxx(long m)减少时间系列方法
Instant plusXxx(long m)增加时间系列方法
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZoneDateTimeDemo {
    public static void main(String[] args) {
        //获取当前时间(当前时区)的ZoneDateTime对象
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println(now);        //2022-12-06T23:07:02.942927900+08:00[Asia/Shanghai]

        //获取指定时区指定年月日等的ZoneDateTime对象
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime of = ZonedDateTime.of(2022, 11, 20, 8, 57, 23, 0, zoneId);
        System.out.println(of);         //2022-11-20T08:57:23+08:00[Asia/Shanghai]

        //获取指定时间戳指定时区的ZoneDateTime对象
        Instant instant = Instant.ofEpochSecond(0L);
        ZonedDateTime ofInstant = ZonedDateTime.ofInstant(instant, zoneId);
        System.out.println(ofInstant);  //1970-01-01T08:00+08:00[Asia/Shanghai]
    }
}

4 DateTimeFormatter 解析与格式化

导包:java.time.format.DateTimeFormatter

常用方法

方法说明
static DateTimeFormatter ofPattern(时间格式)获取指定格式的格式器对象
String format(时间对象)将给定日期对象按照格式器格式化为字符串
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterDemo {
    public static void main(String[] args) {
        //获取带时区的时间对象
        ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(time);	//2022-12-07T10:16:20.808603100+08:00[Asia/Shanghai]

        //获取指定格式的格式器对象
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");

        //将给定日期对象按照格式器格式化为字符串
        String result = timeFormatter.format(time);
        System.out.println(result);	//2022-12-07 10:16:20 周三 上午
    }
}

5 日历类 LocalDate、LocalTime、LocalDateTime

LocalDate 能表示:年、月、日

LocalTime 能表示:时、分、秒、纳秒

LocalDateTime 能表示:年、月、日、时、分、秒、纳秒

导包:java.time.Xxx

常用方法

方法说明
static LocalXxx now()获取当前时间(当前时区)的LocalXxx对象
static LocalXxx of(int y M d H m S)获取指定时区指定年月日时分秒的LocalXxx对象
getXxx(int field)获取给定日历对象中指定的字段(年月日等)的值
isBefore/isAfter(LocalXxx l)判断调用者时间是否在参数时间的前/后
withXxx(long m)修改时间
minusXxx(long m)减少时间
plusXxx(long m)增加时间
isLeapYear()判断日历对象是不是闰年

6 Duration、Period 工具类

导包:java.time.Xxx

常用方法

Duration方法
static Duration between(Temporal start, Temporal end)获取时间间隔Duration的对象
long toXxx()将时间间隔转换为Xxx形式表示
Period方法说明
static Period between(LocalDate start, LocalDate end)获取时间间隔Period的对象
int getXxx()获取时间间隔对象的某个字段(年月日等)

7 ChronoUnit 工具类

ChronoUnit能用任意单位表示时间间隔

*方法:*public long between(Temporal start, Temporal end) //获取时间间隔(不是对象)

语法:ChronoUnit.单位.between(Temporal start, Temporal end)

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class ChronoUnit {
    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2020, 8, 10);
        LocalDate end = LocalDate.of(2022, 10, 20);

        System.out.println(ChronoUnit.YEARS.between(start, end)); 	 // 2
        System.out.println(ChronoUnit.MONTHS.between(start, end));	 // 26
        System.out.println(ChronoUnit.DAYS.between(start, end));	 // 801
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值