JDK1.8日期新增功能

一.JDK8 针对日期类进行升级,新增日期的增减方法

 private static void JDK8Datetest() {
        //定义一个字符串的日期及时间
        String s = "2020年11月11日 00:00:00";
        //静态方法返回格式化对象
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        //把字符串时间和格式进行传入
        LocalDateTime parse = LocalDateTime.parse(s, pattern);
        //在当日期的基础上前进一天,得到新的日期
        LocalDateTime localDateTime = parse.plusDays(1);
        //把新的日期通过格式,格式化成字符串
        String date = localDateTime.format(pattern);
        //输出打印
        System.out.println(date);//输出的结果信息是:2020年11月12日 00:00:00 
}

二.JDK8 获取时间对象

         /**
         * 在JDK8之前,处理日期时间,我们主要使用3个类,
         * Date
         * SimpleDateFormat
         * Calendar(等后边稍微介绍)。
         * 这3个类在使用时都或多或少的存在一些问题,比如SimpleDateFormat不是线程安全的,
         * 比如Date和Calendar获取到的月份是0到11,而不是现实生活中的1到12,*/
        Date date1 = new Date();
        System.out.println(date1.getMonth());// jdk1.8之前获取月份信息的打印结果: 9 系统实际月份为10

        //1.jdk8 获取当前系统时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);//2021-10-11T20:12:55.351

        //2.jdk8 获取指定时间
        LocalDateTime time = LocalDateTime.of(2021, 11, 11, 11, 11, 11);
        System.out.println(time);//2021-11-11T11:11:11
        /**
         * 解释说明:
         * 1.now():获取的时间是当前系统的时间
         * 2.of():方法参数可以指定时间
         * */

三.JDK8 获取时间中的每一个值

        /**
         * JDK8 获取时间中的每个值
         * LocalDateTime常用方法:
         * getYear():获取时间中的年分
         * getMoth():获取时间中的月份
         * getMothValue():获取月份1-12月
         * getDayOfYear():获取第几天1-31
         * getDateOfYear()获取一年中的第几天
         * getDayOfWeek():获取星期 英文表示形式 获取的是枚举值
         * getMinute():胡哦去分钟
         * getHour():获取小时
         * */
        LocalDateTime now1 = LocalDateTime.now();
        //获取年份
        int year = now1.getYear();
        System.out.println(year);
        //获取月份 英文表示 获取的是枚举值
        Month month = now1.getMonth();
        System.out.println(month);

        //获取月份 1-12
        int monthValue = now1.getMonthValue();
        System.out.println(monthValue);

        //获取月份中的第几天 1-31
        int dayOfMonth = now1.getDayOfMonth();
        System.out.println(dayOfMonth);

        //获取一年中的第几天 1-366
        int dayOfYear = now1.getDayOfYear();
        System.out.println(dayOfYear);

        // 获取星期 英文表示形式  获取的是枚举值
        DayOfWeek dayOfWeek = now1.getDayOfWeek();
        System.out.println(dayOfWeek);

        //获取分钟
        int minute = now1.getMinute();
        System.out.println(minute);

        //获取小时
        int hour = now1.getHour();
        System.out.println(hour);

四.JDK8 时间转换---只获取年月日/时分秒

         /**
         * JDK8 时间类--转换方法
         * now():获取当前系统时间
         * toLocalDate():只展示时间的年月日
         *toLocalTime():之战时时间的时分秒
         * */
        LocalDateTime now2 = LocalDateTime.now();//获取当前系统时间
        //转换为localDate对象,只需要展示年月日信息
        LocalDate localDate = now2.toLocalDate();
        System.out.println(localDate);//2021-10-11
        //转换为localTime对象,只展示时分秒
        LocalTime localTime = now2.toLocalTime();
        System.out.println(localTime);//20:56:14.835

五.JDK8 时间类格式化与解析

       /**
         * JDK8 时间类-格式化和解析
         * */
        //获取当前系统时间
        LocalDateTime now3 = LocalDateTime.now();
        System.out.println(now3);//2021-10-11T21:00:25.758
        System.out.println("==============================");
        //格式化 需要一个格式化日期对象DateTomeFromatter(格式)
        String format = now.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss"));
        System.out.println(format);//2021年10月11日 21:02:40
        System.out.println("==============================");
        //解析:  String s = "2020年11月11日 0:0:0";//必须和解析的指定格式相同
        String s1 = "2021年11月11日 00:00:00";
        //传入需要解析的日期字符串
        LocalDateTime parse1 = LocalDateTime.parse(s1, DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss"));
        System.out.println(parse1);//2021-11-11T00:00
        System.out.println("==============================");

六.JDK8 时间类-plus系列的方法

    //获取当前系统时间
    LocalDateTime now = LocalDateTime.now();
    //年份+1
    LocalDateTime year = now.plusYears(1); //正数 负数  0都行
    System.out.println(year.getYear());
    //月份+1
    LocalDateTime month = now.plusMonths(1);
    System.out.println(month.getMonthValue());
    //日  时分秒 周  都有该方法 用法都一致

七.JDK8时间类-minus系列的方法

   //反向思维  
	参数是正数 时间向前推 (指的是过去的时间)  
    参数是负数 时间向后推 (指的是将来的时间)
   
    LocalDateTime now = LocalDateTime.now();
 	//传递正数是now的基础上减去一年,传递-1 是在now的基础上加上一年,0没有任何意义
    //得到的是新的时间对象LocalDateTime
	LocalDateTime years = now.minusYears(0);
    System.out.println(years.getYear());

    //传递正数是在now的月份-1
    LocalDateTime month = now.minusMonths(1);
    System.out.println(month.getMonthValue());
    //日 时分秒 周 保持一致

八.JDK8时间类-with系列的方法

    直接修改时间,得到的是新的时间对象LocalDateTime
    LocalDateTime now = LocalDateTime.now();
    //设置年份 传入多少就是多少年  传入负数不会报错
    LocalDateTime year = now.withYear(1);
    System.out.println(year);
    //设置月份传入的值 但是要在1-12月份
    LocalDateTime month = now.withMonth(12);
    System.out.println(month.getMonthValue());

    //设置一个月中的第几天
    LocalDateTime day = now.withDayOfMonth(15);
    System.out.println(day);

    //显示一年的第几天  显示格式该年的几月几日
    LocalDateTime dayOfYear = now.withDayOfYear(112);
    System.out.println(dayOfYear);	

九.JDK8时间类-时间间隔对象

        LocalDate date1 = LocalDate.of(2020, 12, 21);
        LocalDate date2 = LocalDate.of(2022, 11, 25);
        Period between1 = Period.between(date1, date2);
        System.out.println(between1);
		//P1Y11M4D  
		//P时间间隔对象 1Y 表示一年 11M 表示11个月 4D表示 4天
		// 获取这段时的年
        int years = between1.getYears();
        System.out.println(years);
		//获取这段时的月份
        int months = between1.getMonths();
        System.out.println(months);
		//获取这段时间的天数
        int days = between1.getDays();
        System.out.println(days);
		//表示 这两个时间段中一共间隔了多少个月
        long l = between1.toTotalMonths();
        System.out.println(l);


    LocalDateTime date1 = LocalDateTime.of(2020, 12, 21, 11, 11, 11);
    LocalDateTime date2 = LocalDateTime.of(2020, 12, 21, 12, 12, 12);
    Duration duration = Duration.between(date1, date2);
    System.out.println(duration);
	//PT1H1M1S PT间隔对象 1H 间隔一小时  1M 间隔1分钟 1S 间隔1秒

    long l = duration.toSeconds();
    System.out.println(l);//3661  间隔的秒数
    long l1 = duration.toMillis();
    System.out.println(l1);//3661000  间隔的毫秒
    long l2 = duration.toNanos();
    System.out.println(l2);//3661000000000  //间隔的纳秒

十.JDK8时间类-小结

JDK1.8 前日期函数API

1 java.lang.System类
    public static native long currentTimeMillis();
    用于返回当前时间与1970年1月1日0:0:0之间以毫秒为单位的时间差
   
2 java.util.Date类
    两个构造器
    new Date(); —> 当前时间
    new Date(Long 毫秒数) — > 根据毫秒数创建指定日期

    两个方法的使用
    toString() 显示当前的年,月,日,时,分,秒
    getTime() 获取当前date对象的对应的毫秒数(时间戳)

3 java.text.SimpleDateFormat类
    Date类的API不易于国际化,大部分被废弃,
    SimpleDateFormat类 一个与语言环境有关的方式来格式化和解析时间的具体类
    format(Date d) 方法 按照具体的格式格式化时间
    parse(String s)  方法将字符串解析成时间
    
4 java.util.Calendar 日历类
    获取Calendar的实例
    使用其子类 (GregorianCalendar)的对象
    使用Calendar.getInstance()获取Calendar实例
    常用方法
    1,set()
    calendar.set(Calendar.DAY_OF_MONTH,22) —> 将日期设置为这个月的第几天
    
    2,get()
    calendar.get(Calendar.DAY_OF_MONTH)----->这个月的第几天,返回值是int
    calendar.get(Calendar.DAY_OF_YEAR) —>这一年的第几天
   
    3,add()
    calendar.add(Calendar.DAY_OF_MONTH,3) ----> 在现有的日期上加3天
    
    4,getTime()
    calendar.getTime(); —> 返回Date()
    
    5,setTime()
    calendar.setTime(new Date()) — > 将日期设置为某日期    

JDK1.8 后日期函数API

可变性 : 像日期和时间这样的类应该是不可变的,---->返回一个值,原来的对象不变
偏移性 : Date中的年份是从1900开始的,而月份是从0开始的
日期表示需要减 new Date(2020-1900,9-1,8) 这样才可以表示2020年9月8日
格式化; 格式化日期只对Date有用,Calendar则不行
线程不安全的,不能处理闰秒等
Java8 吸收了Joda-Time(java日期处理时间库)精华,开启了新的API,新的java.time包含了如下子类
    本地日期 LocalDate
    本地时间 LocalTime
    本地日期时间 LocalDateTime
    时区 ZonedDateTime
    持续时间 DurationJoda-Time
    
1 JDK8 的新日期的API
    java.time -包含值对象的基础包
    java.time.chrono - 提供不同日历系统的访问
    java.time.format - 格式化和解析时间和日期
    java.time.temporal - 包含底层框架和扩展特性
    java.time.zone - 包含时区支持的类    

2 LocalDate,LocalTime,LocalDateTime
    获取当前时间
   LocalDate localDate = LocalDate.now();  //年月日`
   LocalTime localTime = LocalTime.now();  //日分秒
   LocalDateTime localDateTime = LocalDateTime.now();

   // LocalDateTime  使用频率较高
   // of() 设置指定的年月日时分秒  体现不偏移性
   LocalDateTime dateTime = LocalDateTime.of(2019, 04, 10, 23, 03);
   // plus系列  时间的加减
   // minus系列 时间的加减
   // wiht系列  设置时间

3 Instant 瞬时  时间戳  1970年1月1日 00:00:00到某个时间之间的毫秒值
   时间线上的一个瞬时点,这可能用于记录应用程序的事件时间戳
   Instant的精度可以达到纳秒级   
    // Instant 和java.util.Date 很像

    // 获取本初子午线的标准时间
    Instant now = Instant.now();
    System.out.println(now);    

    // 东八区的时间       偏移量
    OffsetDateTime offsetDateTime =now.atOffset(ZoneOffset.ofHours(8));
    System.out.println(offsetDateTime);

    //距离1970年1月1日0:0:0(UTC) 开始的毫秒数
    long l = now.toEpochMilli();
    System.out.println(l);

4 java.time.format.DateTimeFormatter 格式化或者解析时间
   自定义格式
   DateTimeFormatter pattern = DateTimeFormatter.ofPattern()("yyyy-MM-dd hh:mm:ss") -->和SimpleDateFormat相似    
   
5 持续时间:Duration,用于计算两个"时间"的间隔
  日期间隔: Period ,用于计算两个"日期"的间隔 

应用场景

1 LocalDate类由一个isLeapYear()方法来返回当前LocalDate对应的那年是否是闰年    
   boolean leapYear = LocalDate.now().isLeapYear();

2 在java中如何判断某个日期在另一个日期的前面还是后面,
  如何判断某个日期在另一个日期的前面还是后面或者相等,
  在java8中,LocalDate类中使用isBefore()、
  isAfter()、equals()方法来比较两个日期。
  如果调用方法的那个日期比给定的日期要早的话,isBefore()方法会返回true

3 在java8中检查两个日期是否相等
  LocalDate重写了equals方法来进行日期的比较

4 在java8中如何检查重复事件,比如生日
  在java中还有一个与时间日期相关的任务就是检查重复事件,比如每月的账单日
  如何在java中判断是否是某个节日或者重复事件,使用MonthDay类。这个类由月日组合,不   包含年信息,
  可以用来代表每年重复出现的一些日期或其他组合。他和新的日期库中的其他类一样也都是不可   变且线程安全的,并且它还是一个值类(value class)。

5 如何表示固定的日期,比如信用卡过期时间
  正如MonthDay表示的是某个重复出现的日子,YearMonth是另外一个组合,代表的是像信用   卡还款日,定期存款到期日,
  options到期日这类的日期。你可以用这个类找出这个月有多少天,
  LengthOfMonth()这个方法返回的是这个YearMonth实例有多少天,这对于检查2月是否润2    月很有用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值