Java基础——常用API(时间类)

一、JDK8版本以前的时间类(了解)

       Date 类:代表的是日期和时间
                1. 构造方法 :
                        public Date() : 将当前时间, 封装为Date日期对象
                        

                        public Date(long time) : 把时间毫秒值转换成Date日期对象(1000后面的L代表的是
                                                               long类型)
                        
                2. 常见方法 :
                        public long getTime() : 返回从1970年1月1日 00:00:00走到此刻的总的毫秒数
                        
                        
                        public void setTime(long time) : 设置日期对象的时间为当前时间毫秒值对应的时间
                        
        SimpleDateFormat 类:用于日期格式化
               1. 构造方法 :
                        public SimpleDateFormat() : 创建一个日期格式化对象, 使用 [默认模式]
                        
                        public SimpleDateFormat(String pattern) : 创建一个日期格式化对象, [手动指定模
                                                                                            式]
                        
                2. 常用方法 :
                        public final String format(Date date) : 将日期对象, 转换为字符串
                        public public final Date parse(String source) : 将日期字符串, 解析为日期对象
                        

        Calendar 类:代表的是系统此刻时间对应的日历,通过它可以单独获取、修改时间中的年、
                              月、日、时、分、秒等。
                1. 创建对象 :
                        public static Calendar getInstance() : 获取当前时间的日历对象
                2. 常用方法 :
                        public int get(int field) : 取日历中的某个字段信息
                        get方法的参数 : Calendar类中的静态常量
                        Calendar.YEAR : 获取年份信息
                        Calendar.MONTH : 月份是0~11, 记得做+1操作
                        Calendar.DAY_OF_MONTH : 获取日
                        Calendar.DAY_OF_WEEK : 获取星期, 获取星期的时候, 需要提前设计一个数组
                        Calendar.DAY_OF_YEAR : 获取一年中的第几天
                        public void set(int field,int value) : 修改日历的某个字段信息
                        public void add(int field,int amount) : 为某个字段增加/减少指定的值
                        

 二、JDK8版本以后的时间类
        1.日历类
         
LocalDate:代表本地日期(年、月、日、星期)
          LocalTime:代表本地时间(时、分、秒、纳秒)
          LocalDateTime:代表本地日期、时间(年、月、日、星期、时、分、秒、纳秒)
                 ① 对象的创建方式:
                        1) now() : 当前时间
                        2)of(...) : 设置时间
                        LocalDateTime 转换LocalDate, LocalTime
                                1)toLocalDate()
                                2)toLocalTime()
                ② 
获取的相关方法
                        getXxx();
                        getYear() getMonth() getMonthValue() ...
                ③ 修改的相关方法
                        withXxx:修改
                        minusXxx:减
                        plusXxx: 加 
                ④ 注意:
                        LocalDateTime 、LocalDate 、LocalTime 都是不可变的
                        调用修改的相关方法, 返回的都是新的对象
         
 例:
        (以LocalDateTime为例,其他两个类似,LocalDate没有时分秒,LocalTime没有年月日)
           
// 创建日期 时间
           LocalDateTime nowDateTime = LocalDateTime.now();
           System.out.println("今天是:" + nowDateTime);
           // 获取年
            int year = nowDateTime.getYear();
          // 获取月
            int monthValue = nowDateTime.getMonthValue();
         //获取 日
           int dayOfMonth = nowDateTime.getDayOfMonth();
         // 获取时
         int hour = nowDateTime.getHour();
         // 获取分
         int minute = nowDateTime.getMinute();
         //获取 秒
         int second = nowDateTime.getSecond();
         // 获取纳秒
         int nano = nowDateTime.getNano();
        // 日 : 当年的第几天
        int dayOfYear = nowDateTime.getDayOfYear();
        // 星期
        //返回的是英文,例如:THURSDAY

        DayOfWeek dayOfWeek = nowDateTime.getDayOfWeek();
        //返回的是数字 例如:4
        int weekValue = nowDateTime.getDayOfWeek().getValue();
        // 月份
        //返回的是英文,例如:THURSDAY

        Month month = nowDateTime.getMonth();
        //返回的是数字 例如:8
        int monthValue = nowDateTime.getMonth().getValue();
       // minus : 减去
        System.out.println("减一小时:" + nowTime.minusHours(1));
        System.out.println("减一分钟:" +nowTime.minusMinutes(1));
        System.out.println("减一秒钟:" +nowTime.minusSeconds(1));
        System.out.println("减一纳秒:" +nowTime.minusNanos(1));
        // plus : 加
        System.out.println("加一小时:" + nowTime.plusHours(1));
        System.out.println("加一分钟:" + nowTime.plusMinutes(1));
        System.out.println("加一秒钟:" + nowTime.plusSeconds(1));
        System.out.println("加一纳秒:" + nowTime.plusNanos(1));
        // with : 设置、修改
        System.out.println(nowTime.withYear(2008));
        System.out.println(nowTime.withMonth(8));
        System.out.println(nowTime.withDayOfMonth(8));
        System.out.println(nowTime.withHour(8));
        System.out.println(nowTime.withMinute(8));
        System.out.println(nowTime.withSecond(8));
        System.out.println(nowTime.withNano(8));

        LocalDate myDate = LocalDate.of(2008, 8, 8);
        LocalDate nowDate = LocalDate.now();
        //2008-08-08是否在nowDate之前?
        System.out.println(myDate + "是否在" + nowDate + "之前? " + myDate.isBefore(nowDate));
        //2008-08-08是否在nowDate之后?
        System.out.println(myDate + "是否在" + nowDate + "之后? " + myDate.isAfter(nowDate));
        // 判断两个时间是否相同
        System.out.println(myDate.equals(nowDate));
        2.日期格式化类
           DateTimeFormatter 类:
用于时间的格式化和解析
                1. 对象的获取 :
                        static DateTimeFormatter ofPattern(格式) : 获取格式对象
                2. 格式化 :
                        String format(时间对象) : 按照指定方式格式化
                3. 解析 :
                        LocalDateTime.parse("解析字符串", 格式化对象);
                        LocalDate.parse("解析字符串", 格式化对象);
                        LocalTime.parse("解析字符串", 格式化对象);
                例:
                     LocalDateTime now = LocalDateTime.now();
                     System.out.println("格式化之前:" + now);
                     // 获取格式化对象
                     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日");
                     // 格式化
                     String result = formatter.format(now);
                     System.out.println("格式化之后:" + result);
                    // 解析
                    String time = "2008年08月08日";
                    LocalDate parse = LocalDate.parse(time, formatter);
                    System.out.println(parse);

                    
        3.时间类
            Instant :时间戳/时间线
                
用于表示时间的对象,  类似之前所学习的 Date
                Instant now = Instant.now();
                System.out.println("当前的时间戳是:" + now);//会有时差
                
Instant类常见方法 :
                        static Instant now() : 获取当前时间的Instant对象(标准时间)
                        static Instant ofXxxx(long epochMilli) : 根据(秒/毫秒/纳秒)获取Instant对象
                        ZonedDateTime atZone(ZoneId zone) : 指定时区
                        boolean isXxx(Instant otherInstant) : 判断系列的方法
                        Instant minusXxx(long millisToSubtract) : 减少时间系列的方法
                        Instant plusXxx(long millisToSubtract) : 增加时间系列的方法
            ZoneId : 时区
   
                常见方法 :
                        1. static Set<String> getAvailableZoneIds() : 获取Java中支持的所有时区
                        2. static ZoneId systemDefault() : 获取系统默认时区
                        3. static ZoneId of(String zoneId) : 获取一个指定时区
            ZonedDateTime : 带时区的时间
                
//指定时区后就不会有时差了
                ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
                System.out.println(zonedDateTime);
                常用方法:
                 static ZonedDateTime now() : 获取当前时间的ZonedDateTime对象
                 static ZonedDateTime ofXxxx(...) : 获取指定时间的ZonedDateTime对象
                 ZonedDateTime withXxx(时间) : 修改时间系列的方法
                 ZonedDateTime minusXxx(时间) : 减少时间系列的方法
                 ZonedDateTime plusXxx(时间) : 增加时间系列的方法
                注释:Xxx:Year、Month、Day等
        4.工具类
                Period : 时间间隔(年,月,日)
                        
// 此刻年月日
                        LocalDate today = LocalDate.now();
                        System.out.println(today);
                        // 昨天年月日
                        LocalDate otherDate = LocalDate.of(2023, 2, 4);
                        System.out.println(otherDate);
                        //Period对象表示时间的间隔对象
                        Period period = Period.between(today, otherDate); // 第二个参数减第一个参数
                        System.out.println(period.getYears()); // 间隔多少年
                        System.out.println(period.getMonths()); // 间隔的月份
                        System.out.println(period.getDays()); // 间隔的天数
                        System.out.println(period.toTotalMonths()); // 总月份

                Duration : 时间间隔(时、分、秒,纳秒)
                        
// 此刻日期时间对象
                        LocalDateTime today = LocalDateTime.now();
                        System.out.println(today);
                        // 昨天的日期时间对象
                        LocalDateTime otherDate = LocalDateTime.of(2023, 2, 4, 0, 0, 0);
                        System.out.println(otherDate);
                Duration duration = Duration.between(otherDate, today); // 第二个参数减第一个参数
                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()); // 两个时间差的纳秒数
                ChronoUnit : 时间间隔 (所有单位)
               
// 本地日期时间对象:此刻的
                LocalDateTime today = LocalDateTime.now();
                System.out.println(today);
                // 生日时间
                LocalDateTime birthDate = LocalDateTime.of(2023, 2, 4,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));

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值