JAVA学习笔记-常用类(三)——有关时间的API

重点掌握JDK8.0新引入的关于时间的包,且常用的开发中就够用了

一.JDK8之前日期和时间的API测试

System类中的currentTimeMillis():返回当前时间与1970年1月1日e时e分0秒之间以毫秒为单位的时间差。称为时间戳

java.util.Date类
/—java.sqL.Date类

  1. 两个构造器的使用

    构造器一: Date():创建一个当前时间的Date对象
    构造器二:创建指定毫秒数的Date对象
    
  2. 两个方法的使用

    toString():显示当前年,月,日,时,分,秒
    getTime():获取当前Date对象对应的毫秒数。(时间戳)
    
  3. java.sqL. Date对应着数据库中的日期类型的变量
    如何实例化
    如何将java.utiL.Date对象转换为java.sqL.Date对象

//构造器一: Date():创建一个当前时间的Date对象
        Date date1 = new Date();
        System.out.println(date1.toString());//当前时间
        System.out.println(date1.getTime());//毫秒数
        //构造器二:创建指定毫秒数的Date对象
        Date date2 = new Date(1627392325804L);
        System.out.println(date2.toString());

        //创建java.sql.Date对象
        java.sql. Date date3 = new java.sql.Date(3523532534L);
        System.out.println(date3);
        //java.utiL.Date对象转换为java.sqL.Date对象
        java.sql. Date date4= new java.sql.Date(date1.getTime());
        System.out.println(date4);

SimpleDateFormat的使用:SimpleDateFormat对日期Date类的格式化和解析
1.两个操作:
1.1格式化:日期—>字符串-------------->格式化(format()):日期—>字符串
1.2解析:格式化的逆过程,字符串—>日期------------>解析(parse()):格式化的逆过程,字符串—>日期

2.SimpLeDateFormat的实例化

//实例化SimpleDateFormat
        SimpleDateFormat sdf=new SimpleDateFormat();//默认空参构造器

        //格式化(format()):日期--->字符串
        Date date = new Date();
        String format=sdf.format(date);
        System.out.println(format);//输出当前时间

        //解析(parse()):格式化的逆过程,字符串--->日期
        String str = "19-12-18 上午11:43" ;
        Date parse1= sdf.parse(str);
        System.out.println(parse1);

Calendar日历类(抽象类)的使用
问题:
可变性:像日期和时间这样的类应该是不可变的。
偏移性:Date中的年份是从190O开始的,而月份都从O开始。
格式化:格式化只对Date有用,Calendar则不行。
此外,它们也不是线程安全的;不能处理闰秒等。

1.实例化
方式一:创建其子类(GregorianCalendar)的对象

方式二:调用其静态方法getInstance()获取当前时间

2.Calendar常用方法

get()

set()

add()–>可变性

getTime():日历类—>Date

setTime():Date—>日历类

@Test
    public void testCalendar() {
    
    
    	//实例化
        //方式一:创建其子类(GregorianCalendar)的对象
        //方式二:调用其静态方法getInstance()获取当前时间
        Calendar calendar = Calendar.getInstance();//GregorianCalendar
        //2.常用方法
        //get()
        int days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);//29
        System.out.println(Calendar.DAY_OF_YEAR);

        //set()
        calendar.set(Calendar.DAY_OF_MONTH, 29);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        // add()-->可变性
        calendar.add(Calendar.DAY_OF_MONTH, 3);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        //getTime():日历类--->Date
        Date date = calendar.getTime();
        System.out.println(date);

        //setTime():Date--->日历类
        Date date1 = new Date();
        calendar.setTime(date1);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
    }

二.JDK8.0新引入的关于时间的包(能够很好的解决前面说到的偏移量问题)

​ java.time-包含值对象的基础包
​ java.time.chrono-提供对不同的日历系统的访问
​ java.time.format-格式化和解析时间和日期
​ java.time.temporal-包括底层框架和扩展特性
​ java.time.zone -包含时区支持的类

主要类的使用LocalDate,LocalTime,LocalDateTime的使用
说明:
LocalDateTime相较于LocalDate,LocalTime使用频率较高

LocalDate,LocalTime,LocalDateTime的常用方法:

now():创建对象,获取当前的日期,时间,日期+时间

of():设置指定的年,月,日,时,分,秒。没有偏移量

getXxx()返回相关属性

体现不可变性withXxx()∶设置相关的属性

不可变性(plusXxx()增加)

不可变性(minusXxx()减少)

 @Test
    public void test(){
    
    
        //now():获取当前的日期,时间,日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        //of():设置指定的年,月,日,时,分,秒。没有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2021, 7, 28, 19, 28, 54);
        System.out.println(localDateTime1);

        //getXxx()返回相关属性
        System.out.println(localDate.getDayOfWeek());
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfYear());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getMinute());

        //体现不可变性
        //withXxx()∶设置相关的属性
        LocalDate localDate1 = localDate.withDayOfMonth(22);
        System. out.println( localDate);
        System.out.println( localDate1);

        LocalDateTime localDateTime2 = localDateTime.withHour(4);
        System.out.println( localDateTime) ;
        System.out.println( localDateTime2);

        //不可变性(plusXxx()增加)
        LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
        System. out.println( localDateTime) ;
        System.out.println( localDateTime3);

        //不可变性(minusXxx()减少)
        LocalDateTime localDateTime4 = localDateTime.minusDays(6);
        System.out.println( localDateTime) ;
        System.out.println( localDateTime4);

    }

Instant类的使用
类似于java.util.Date(感觉不是很常用)

 		//now()∶获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);//2021-07-29T12:30:09.773Z

        //添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2021-07-29T20:30:09.773+08:00

        //toEpochMilli:获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数
        long milli =instant.toEpochMilli();
        System.out.println(milli);//1627561809773

        //ofEpoch/MiLLi():通过给定的毫秒数,获取Instant实例-->Date类的getTime()
        Instant instant1 = Instant.ofEpochMilli(1627561809773L);
        System.out.println(instant1);

DateTimeFormatter:格式化或解析日期、时间**
类似于SimpleDateFormat**

格式化(format()):日期—>字符串
解析(parse()):字符串—>日期

预定义的标准格式。如:iso_LOCAL_DATE_TIME;lSo_LOCAL_DATE;ISO_LOCAL_TIME
本地化相关的格式。如: ofLocalizedDateTime(FormatStyle.LONG)
自定义的格式。如: ofPattern(“yyyy-MM-dd hh:mm:ss E")---->重点掌握

 @Test
    public void test2(){
    
    
        LocalDateTime localDateTime=LocalDateTime.now();
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        //1.预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;

            //格式化(format()):日期--->字符串
        String str1=formatter.format(localDateTime);//2021-07-30T14:48:21.936
        System.out.println(localDateTime);//2021-07-30T14:48:21.936
        System.out.println(str1);
            //解析(parse()):字符串--->日期
        TemporalAccessor parse1 = formatter.parse("2021-07-30T14:48:21.936");
        System.out.println(parse1);

        /*
        方式二:
        本地化相关的格式一。如: ofLocalizedDateTime()
        FormatStyLe.LONG /FormatStyLe.MEDIUM /FormatStyLe.SHORT :适用于LocalDateTime
         */
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
            //格式化(format())
        String str2=formatter1.format(localDateTime);
        System.out.println(str2);//2021年7月30日 下午03时04分03秒

         /*
         本地化相关的格式二。如: ofLocalizedDate()
         FormatStyLe.FULL / FormatStyLe.LONG / FormatStyLe.MEDIUM /FormatStyLe.SHORT:适用于LocalDate
         */
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
        String str3= formatter2.format(localDate);
        System.out.println(str3);//2021-7-30


        //(重点掌握)3.自定义的格式。如: ofPattern(“yyyy-MM-dd hh:mm:ss E")
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss E");
            //格式化(format())
        String str4= formatter3.format(localDateTime);
        System.out.println(str4);//2021-07-30 03:11:27

            //解析(parse())
        TemporalAccessor parse2= formatter3.parse("2021-07-30 03:11:27 星期五");
        System.out.println(parse2);
    }

猜你喜欢

转载自blog.csdn.net/m0_46450708/article/details/119617179