jdk 8前后日期时间API的测试

导读:本篇文章讲解 jdk 8前后日期时间API的测试,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

JDK 8之前日期和时间的API测试

System类中的currentTimeMillis()

    //1.System类中的currentTimeMillis()
    @Test
    public void test1(){
        long time = System.currentTimeMillis();
        //返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
        //称为时间戳
        System.out.println(time);
    }

java.util.Date类

    /*
    java.util.Date类
           |---java.sql.Date类       子父类的关系

    1.两个构造器的使用
        >构造器一:Date():创建一个对应当前时间的Date对象
        >构造器二:创建指定毫秒数的Date对象
    2.两个方法的使用
        >toString():显示当前的年、月、日、时、分、秒
        >getTime():获取当前Date对象对应的毫秒数。(时间戳)

    3. java.sql.Date对应着数据库中的日期类型的变量
        >如何实例化
        >如何将java.util.Date对象转换为java.sql.Date对象
     */
    @Test
    public void test2(){
        //构造器一:Date():创建一个对应当前时间的Date对象
        Date date1 = new Date();
        System.out.println(date1.toString());//Sat Jan 07 11:47:29 CST 2023

        System.out.println(date1.getTime());//1673063249304是毫秒数

        //构造器二:创建指定毫秒数的Date对象
        Date date2 = new Date(155030620410L);
        System.out.println(date2.toString());//Sat Nov 30 16:03:40 CST 1974     标准的UTC时间,CST表示了偏移量+800

        //创建java.sql.Date对象
        java.sql.Date date3 = new java.sql.Date(35235325345L);
        System.out.println(date3);//1971-02-13

        //如何将java.util.Date对象转换为java.sql.Date对象
        //情况一:
//        Date date4 = new java.sql.Date(2343243242323L);//子类赋给父类,多态
//        java.sql.Date date5 = (java.sql.Date) date4;
        //情况二:
        Date date6 = new Date();
        java.sql.Date date7 = new java.sql.Date(date6.getTime());
        System.out.println(date7);//2023-01-07

    }

 java.text.SimpleDateFormat类

  • 它允许进行格式化:日期–>文本;解析:文本–>日期
public class DateTimeTest {
    /*
    SimpleDateFormat的使用:SimpleDateFormat对日期Date类的格式化和解析

    1.两个操作:
    1.1 格式化:日期 --->字符串
    1.2 解析:格式化的逆过程,字符串 ---> 日期

    2.SimpleDateFormat的实例化

     */
    @Test
    public void testSimpleDateFormat() throws ParseException {
        //实例化SimpleDateFormat:使用默认的构造器
        SimpleDateFormat sdf = new SimpleDateFormat();

        //格式化:日期 --->字符串
        Date date = new Date();
        System.out.println(date);//Tue Mar 29 10:49:34 CST 2022

        String format = sdf.format(date);
        System.out.println(format);//22-3-29 上午10:49

        System.out.println("*************************");
        //解析:格式化的逆过程,字符串 ---> 日期
        String str = "22-3-29 上午10:49";
        Date date1 = sdf.parse(str);
        System.out.println(date1);//Tue Mar 29 10:49:00 CST 2022

        //*************按照指定的方式格式化和解析:调用带参的构造器*****************
//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //格式化
        String format1 = sdf1.format(date);
        System.out.println(format1);//2022-03-13 08:49:39
        //解析:要求字符串必须是符合SimpleDateFormat识别的格式(通过构造器参数体现),
        //否则,抛异常
        Date date2 = sdf1.parse("2020-02-18 11:48:27");
        System.out.println(date2);//Tue Feb 18 11:48:27 CST 2020
    }

    /*
    练习一:字符串"2020-09-08"转换为java.sql.Date

    练习二:"三天打渔两天晒网"   1990-01-01  xxxx-xx-xx 打渔?晒网?

    举例:2020-09-08 ? 总天数

    总天数 % 5 == 1,2,3 : 打渔
    总天数 % 5 == 4,0 : 晒网

    总天数的计算?
    方式一:( date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24) + 1
    方式二:1990-01-01  --> 2019-12-31  +  2020-01-01 -->2020-09-08
     */
    @Test
    public void testExer() throws ParseException {
        String birth = "2020-09-08";

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf1.parse(birth);
//        System.out.println(date);//Tue Sep 08 00:00:00 CST 2020

        java.sql.Date birthDate = new java.sql.Date(date.getTime());
        System.out.println(birthDate);//2020-09-08
    }
}

用Date()类实例化对象,但是最重要的是偏移量的问题

public void testDate(){
  //偏移量
  Date date1 = new Date(2022 - 1900,3 - 1,29);//这样表示出来的才是2020年9月8号
  System.out.println(date1);//Tue Mar 29 00:00:00 CST 2022
}
  • LocalDate、LocalTime、LocalDateTime 的使用

1.LocalDateTime相较于LocalDate、LocalTime,使用频率要高
2.类似于Calendar
    public void test1(){
        //now():获取当前的日期、时间、日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDate);//2022-03-14
        System.out.println(localTime);//11:43:17.485
        System.out.println(localDateTime);//2022-03-14T11:43:17.485

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

        //of():设置指定的年、月、日、时、分、秒。没有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2022, 3, 29, 11, 15, 25);
        System.out.println(localDateTime1);//2022-03-29T11:15:25


        //getXxx():获取相关的属性
        System.out.println(localDateTime.getDayOfMonth());//29
        System.out.println(localDateTime.getDayOfWeek());//TUESDAY
        System.out.println(localDateTime.getMonth());//MARCH
        System.out.println(localDateTime.getMonthValue());//3
        System.out.println(localDateTime.getMinute());//16

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

        //体现不可变性
        //withXxx():设置相关的属性
        LocalDate localDate1 = localDate.withDayOfMonth(30);
        System.out.println(localDate);//2022-03-29
        System.out.println(localDate1);//2022-03-30


        LocalDateTime localDateTime2 = localDateTime.withHour(4);
        System.out.println(localDateTime);//2022-03-29T11:16:15.318
        System.out.println(localDateTime2);//2022-03-29T04:16:15.318

        //不可变性
        LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
        System.out.println(localDateTime);//2022-03-14T12:08:25.035
        System.out.println(localDateTime3);//2022-06-14T12:08:25.035

        LocalDateTime localDateTime4 = localDateTime.minusDays(6);
        System.out.println(localDateTime);//2022-03-14T12:08:25.035
        System.out.println(localDateTime4);//2022-03-08T12:08:25.035
    }
  •  Instant的使用

    类似于 java.util.Date类

    public void test2(){
        //now():获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);//2022-03-30T04:50:36.438Z

        //添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2022-03-30T12:50:36.438+08:00

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

        //ofEpochMilli():通过给定的毫秒数,获取Instant实例  -->Date(long millis)
        Instant instant1 = Instant.ofEpochMilli(1648615836438L);
        System.out.println(instant1);//2022-03-30T04:50:36.438Z
    }

DateTimeFormatter:格式化或解析日期、时间

    public void test3(){
//        方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期-->字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String str1 = formatter.format(localDateTime);
        System.out.println(localDateTime);//2022-03-14T14:19:36.914
        System.out.println(str1);//2022-03-14T14:19:36.914

        //解析:字符串 -->日期
        TemporalAccessor parse = formatter.parse("2019-02-18T15:42:18.797");
        System.out.println(parse);//{},ISO resolved to 2019-02-18T15:42:18.797

//        方式二:
//        本地化相关的格式。如:ofLocalizedDateTime()
//        FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        //格式化
        String str2 = formatter1.format(localDateTime);
        System.out.println(str2);//2022年3月14日 下午02时19分36秒


//      本地化相关的格式。如:ofLocalizedDate()
//      FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT : 适用于LocalDate
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
        //格式化
        String str3 = formatter2.format(LocalDate.now());
        System.out.println(str3);//2022-3-14


//       重点: 方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String str4 = formatter3.format(LocalDateTime.now());
        System.out.println(str4);//2022-03-14 02:19:36

        //解析
        TemporalAccessor accessor = formatter3.parse("2019-02-18 03:52:09");
        System.out.println(accessor);//{MinuteOfHour=52, HourOfAmPm=3, NanoOfSecond=0, SecondOfMinute=9, MicroOfSecond=0, MilliOfSecond=0},ISO resolved to 2019-02-18

    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/83312.html

(0)

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!