JDK8新增的时间类

本文详细介绍了Java中ZoneId用于时区管理,Instant表示时间戳,ZoneDateTime处理带时区的时间,DateTimeFormatter进行时间和日期格式化,以及Calendar类的功能,包括日期和时间操作。同时涵盖了Duration、Period和ChronoUnit用于时间间隔计算。
摘要由CSDN通过智能技术生成

目录

内容大纲:

        1、Zoneld时区

        2、Instant时间戳

        3、ZoneDateTime带时区的时间

        4、DateTimeFormatter用于时间的格式化和解析

        5、Calendar类:

        6、工具类


内容大纲:

        

        1、Zoneld时区

                

方法名说明
static Set<String>getArailableZoneIds()获取Java中支持的所有时区
static Zoneld systemDefault()获取系统默认时区
static Zoneld of(String Zoneld)获取一个指定时区

        代码如下:

import java.time.ZoneId;
import java.util.Set;

public class Time_zoneIdDEMO {
    public static void main(String[] args) {
        //1、获取所有的时区
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        //显示Java给的时区的数量
        System.out.println(zoneIds.size());

        System.out.println("---------------------------------");

        //打印这些时区
        System.out.println(zoneIds);

        System.out.println("---------------------------------");

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

        System.out.println("---------------------------------");

        //3、获取一个指定时区
        ZoneId zoneId1 = ZoneId.of("Africa/Nairobi");
        System.out.println(zoneId1);
    }
}

运行结果如下:

        2、Instant时间戳

                

方法名说明
static Instant now()获取当前时间的Instant对象(标准时间)
static Instant ofXxx(long epochMilli)根据(s/ms/ns)获取Instant对象
ZonedDateTime atZone(ZoneId zone)

指定时区

boolean isXxx(Instant other Instant)判断系列的方法
Instant minusXxx(long millisToSubtract)减少时间系列的方法
Instant plusXxx(long millisToSubtract)增加时间系列的方法

        代码如下:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Time_instantDEMO {
    public static void main(String[] args) {
        //1、获取当前时间的Instance对象(没有时间区的标准时间)
        Instant now = Instant.now();
        System.out.println(now);

        System.out.println("--------------------------");

        //2、根据(秒,毫秒,纳秒)获取Instance对象
        Instant instant1 = Instant.ofEpochMilli(0L);
        System.out.println(instant1);
        System.out.println("--------------------------");
        Instant instant2 = Instant.ofEpochSecond(1L);
        System.out.println(instant2);
        System.out.println("--------------------------");
        Instant instant3 = Instant.ofEpochSecond(1L,1000000000L);
        System.out.println(instant3);

        System.out.println("--------------------------");

        //3、获取指定时区时间
        ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(time);

        System.out.println("--------------------------");

        //4、isXxx判断
        Instant instant4 = Instant.ofEpochMilli(0L);
        Instant instant5 = Instant.ofEpochMilli(1000L);
        boolean falg = instant4.isBefore(instant5);
        System.out.println(falg);

        System.out.println("--------------------------");

        //minusXxx减少时间
        Instant instant6 = Instant.ofEpochMilli(1000000L);
        System.out.println(instant6);//1970-01-01T00:16:40Z
        Instant instant7 = instant6.minusSeconds(1L);
        System.out.println(instant7);

        System.out.println("---------------------------");
    }
}

                这里需要注意:

        代码的运行结果如下:

        3、ZoneDateTime带时区的时间

方法名说明
static ZonedDateTime now()获取当前时间的zonedDateTime对象
static ZonedDateTime odXxx()获取指定时间的ZonedDateTime对象
ZonedDateTime withXxx(时间)修改时间系列的方法
ZonedDateTime minusXxx(时间)减少时间系列的方法
ZonedDateTime plusXxx(时间)增加时间系列的方法

        代码如下:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Zonedatetime {

    public static void main(String[] args) {
        /*
            static ZonedDateTime now() 获取当前时间的ZonedDateTime对象
            static ZonedDateTime ofXxxx(。。。) 获取指定时间的ZonedDateTime对象
            ZonedDateTime withXxx(时间) 修改时间系列的方法
            ZonedDateTime minusXxx(时间) 减少时间系列的方法
            ZonedDateTime plusXxx(时间) 增加时间系列的方法
         */
        //1.获取当前时间对象(带时区)
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println(now);

        //2.获取指定的时间对象(带时区)1/年月日时分秒纳秒方式指定
        ZonedDateTime time1 = ZonedDateTime.of(2023, 10, 1,
                11, 12, 12, 0, ZoneId.of("Asia/Shanghai"));
        System.out.println(time1);

        //通过Instant + 时区的方式指定获取时间对象
        Instant instant = Instant.ofEpochMilli(0L);
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId);
        System.out.println(time2);


        //3.withXxx 修改时间系列的方法
        ZonedDateTime time3 = time2.withYear(2000);
        System.out.println(time3);

        //4. 减少时间
        ZonedDateTime time4 = time3.minusYears(1);
        System.out.println(time4);

        //5.增加时间
        ZonedDateTime time5 = time4.plusYears(1);
        System.out.println(time5);


    }
}

        运行结果如下:

        4、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 Datetimeformatter {
    public static void main(String[] args) {
         /*
            static DateTimeFormatter ofPattern(格式) 获取格式对象
            String format(时间对象) 按照指定方式格式化
        */
        //获取时间对象
        ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));

        // 解析/格式化器
        DateTimeFormatter dtf1=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm;ss EE a");
        // 格式化
        System.out.println(dtf1.format(time));
    }
}

运行结果如下:

        5、Calendar类:

                

方法名说明
static XXX now()获取当前的对象
static XXX of()获取指定的时间对象
get开头的方法获取日历中的年,月,日,时,分,秒等信息
isBefore,isAfter比较两个LocalDate
with开头的修改时间系列的方法
minus开头的减少时间系列的方法
plus开头的增加时间系列的方法
public LocalDate toLocalDate()LocalDateTime转换成LocalDate对象
public LocalTime toLocalTime()LocalDateTime转换成LocalTime对象

代码如下:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;
public class _LocalDate {
    public static void main(String[] args) {
        //1.获取当前时间的日历对象(包含 年月日)
        LocalDate nowDate = LocalDate.now();
        //System.out.println("今天的日期:" + nowDate);
        //2.获取指定的时间的日历对象
        LocalDate ldDate = LocalDate.of(2023, 1, 1);
        System.out.println("指定日期:" + ldDate);

        System.out.println("=============================");

        //3.get系列方法获取日历中的每一个属性值//获取年
        int year = ldDate.getYear();
        System.out.println("year: " + year);
        //获取月//方式一:
        Month m = ldDate.getMonth();
        System.out.println(m);
        System.out.println(m.getValue());

        //方式二:
        int month = ldDate.getMonthValue();
        System.out.println("month: " + month);


        //获取日
        int day = ldDate.getDayOfMonth();
        System.out.println("day:" + day);

        //获取一年的第几天
        int dayofYear = ldDate.getDayOfYear();
        System.out.println("dayOfYear:" + dayofYear);

        //获取星期
        DayOfWeek dayOfWeek = ldDate.getDayOfWeek();
        System.out.println(dayOfWeek);
        System.out.println(dayOfWeek.getValue());

        //is开头的方法表示判断
        System.out.println(ldDate.isBefore(ldDate));
        System.out.println(ldDate.isAfter(ldDate));

        //with开头的方法表示修改,只能修改年月日
        LocalDate withLocalDate = ldDate.withYear(2000);
        System.out.println(withLocalDate);

        //minus开头的方法表示减少,只能减少年月日
        LocalDate minusLocalDate = ldDate.minusYears(1);
        System.out.println(minusLocalDate);


        //plus开头的方法表示增加,只能增加年月日
        LocalDate plusLocalDate = ldDate.plusDays(1);
        System.out.println(plusLocalDate);

        //-------------
        // 判断今天是否是你的生日
        LocalDate birDate = LocalDate.of(2000, 1, 1);
        LocalDate nowDate1 = LocalDate.now();

        MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth());
        MonthDay nowMd = MonthDay.from(nowDate1);

        System.out.println("今天是你的生日吗? " + birMd.equals(nowMd));//今天是你的生日吗?

    }
}

运行结果如下:

        6、工具类

                Duration:用于计算两个“时间”间隔(秒,纳秒)

                Period:用于计算两个“日期”间隔(年,月,日)

                ChronoUnit:用于计算两个“日期”间隔(所有单位)

        Duration代码:

import java.time.Duration;
import java.time.LocalDateTime;
public class DurationDEMO {
    public static void main(String[] args) {
        // 本地日期时间对象。
        LocalDateTime today = LocalDateTime.now();
        System.out.println(today);

        // 出生的日期时间对象
        LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1, 0, 0, 0);
        System.out.println(birthDate);

        Duration duration = Duration.between(birthDate, today);//第二个参数减第一个参数
        System.out.println("相差的时间间隔对象:" + duration);

        System.out.println("============================================");
        System.out.println(duration.toDays());//两个时间差的天数
        System.out.println(duration.toHours());//两个时间差的小时数
        System.out.println(duration.toMinutes());//两个时间差的分钟数
        System.out.println(duration.toMillis());//两个时间差的毫秒数
        System.out.println(duration.toNanos());//两个时间差的纳秒数
    }
}

运行结果如下:

                Period代码如下:

import java.time.LocalDate;
import java.time.Period;

public class PeriodDEMO {
    public static void main(String[] args) {
        // 当前本地 年月日
        LocalDate today = LocalDate.now();
        System.out.println(today);

        // 生日的 年月日
        LocalDate birthDate = LocalDate.of(2000, 1, 1);
        System.out.println(birthDate);

        Period period = Period.between(birthDate, today);//第二个参数减第一个参数

        System.out.println("相差的时间间隔对象:" + period);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());

        System.out.println(period.toTotalMonths());
    }
}

运行结果如下:

                ChronoUnit代码如下:

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class ChronoUnitDEMO {
    public static void main(String[] args) {
        // 当前时间
        LocalDateTime today = LocalDateTime.now();
        System.out.println(today);
        // 生日时间
        LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1,
                0, 0, 0);
        System.out.println(birthDate);

        System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate, today));
        System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate, today));
        System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today));
        System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate, today));
        System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate, today));
        System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate, today));
        System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate, today));
        System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate, today));
        System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate, today));
        System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate, today));
        System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate, today));
        System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate, today));
        System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between(birthDate, today));
        System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate, today));
        System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate, today));
    }
}

运行结果如下:

  • 26
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java JDK8中,新增了一些时间处理,主要包括以下几个常用: 1. LocalDate:表示日期,可以用来表示年月日的日期,无时区信息。 2. LocalTime:表示时间,可以用来表示时分秒的时间,无日期和时区信息。 3. LocalDateTime:表示日期和时间,可以用来表示年月日时分秒的完整时间,无时区信息。 4. Instant:表示时间戳,可以用来表示从1970年1月1日开始的毫秒数或纳秒数,具有时区信息。 5. Duration:表示时间间隔,可以用来表示两个时间点之间的差距,以秒或纳秒为单位。 6. Period:表示日期间隔,可以用来表示两个日期之间的差距,以年、月或天为单位。 在使用JDK8的时间处理时,可以使用以下方法进行时间转换: 1. 使用LocalDateTime的parse方法将字符串转换为LocalDateTime对象。 2. 使用LocalDateTime的format方法将LocalDateTime对象格式化为指定格式的字符串。 3. 使用DateTimeFormatter来自定义格式化和解析的模式。 4. 使用Instant对象进行时区转换,可以使用atZone方法指定时区。 5. 使用Duration对象进行时间间隔的计算,可以使用toMinutes、toHours、toDays等方法获取不同单位的时间间隔。 6. 使用Period对象进行日期间隔的计算,可以使用getYears、getMonths、getDays等方法获取不同单位的日期间隔。 总结起来,Java JDK8提供了一系列的时间处理,可以方便地进行时间的表示、转换和计算。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值