Java常用API(第三篇)

ZoneId类

ZoneId类可以获取时区的ID

方法作用
systemDefault()静态方法,获取系统默认的时区
getAvaliableZoneIds()静态方法,返回一个Set集合,获取Java支持的所有时区ID
of()静态方法,把某个时区id封装成ZoneId对象
public class Test {
    public static void main(String[] args) {
        //获取系统的默认时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);
        System.out.println("--------------------");

        //获取Java支持的所有时区的字符串
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        for (String id : zoneIds) {
            System.out.println(id);
        }
        System.out.println("--------------------");

        //把某个时区id封装成ZoneId对象
        ZoneId id = ZoneId.of("Asia/Tokyo");
        System.out.println(id);
    }

ZonedDateTime类

获取带时区的日期时间,年月日,时分秒

方法作用
now()静态方法,获取当前系统的时间
now(ZoneId zone)传入一个时区对象,获取指定时区的时间
getXxx()获取数据
withXxx()修改数据
plusXxx()添加数据
minusXxx()减少数据
equals()判断两个对象是否相等
isBefore()判断对象时间是否在指定对象时间之前
isAfter()判断对象时间是否在指定对象时间之后
public class Test {
    public static void main(String[] args) {
        //创建对象
        ZonedDateTime now = ZonedDateTime.now();
        ZonedDateTime now1 = ZonedDateTime.now(ZoneId.systemDefault());

        //获取数据
        int year = now.getYear();
        System.out.println(year);

        //修改数据
        ZonedDateTime withYear = now.withYear(2008);
        System.out.println(withYear);

        //添加数据
        ZonedDateTime plusYears = now.plusYears(10);
        System.out.println(plusYears);

        //减少数据
        ZonedDateTime minusYears = now.minusYears(10);
        System.out.println(minusYears);

        //时间比较
        //判断是否相等
        boolean equals = now.equals(now1);
        System.out.println(equals);

        //判断是否在指定时间之前
        boolean before = now.isBefore(now1);
        System.out.println(before);

        //判断是否在指定时间之后
        boolean after = now.isAfter(now1);
        System.out.println(after);
    }
}

Instant类

Instant类是Java 8中引入的新类,用于表示精确到纳秒级别的时间戳。作用是获取某个瞬间的时间,秒,毫秒,微秒,纳秒

方法作用
now()静态方法,获取当前时间戳对象
getEpochSecond()获取当前对象的总秒数
getNano()获取当前时间后面的纳秒数
plus()增加指定的时间间隔
minus()减少指定的时间间隔
public class Test {
    public static void main(String[] args) {
        //Instant类是Java 8中引入的新类,用于表示精确到纳秒级别的时间戳。
        // 作用是获取某个瞬间的时间,秒,毫秒,微秒,纳秒

        //获取对象
        Instant now = Instant.now();
        System.out.println(now);

        //获取总秒数
        long epochSecond = now.getEpochSecond();
        System.out.println(epochSecond);

        //获取纳秒数
        int nano = now.getNano();
        System.out.println(nano);

        //增加指定的时间间隔
        Instant plus = now.plus(Duration.ofMinutes(10));
        System.out.println(plus);

        //减去指定的时间间隔
        Instant minus = now.minus(Duration.ofMinutes(6));
        System.out.println(minus);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值