目录
  • jdk 8中关于日期和时间的api测试

jdk 8 之前日期和时间的api测试

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

  /*
    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());//sun aug 08 18:07:27 cst 2021
        //构造器二:创建指定毫秒数的date对象
        date  date2 = new date(1534798293927l);
        system.out.println(date2.tostring());//tue aug 21 04:51:33 cst 2018
        //创建java.sql.date对象
        java.sql.date date3 = new java.sql.date(237493269533l);
        system.out.println(date3);//1977-07-12
        //如何将java.util.date对象转换为java.sql.date对象
        //情况一:(面向对象)
//        date date4 = new java.sql.date(23682368236343l);
//        java.sql.date date5 = (java.sql.date)date4;
        //情况二:
        date date6 = new date();
        java.sql.date date7 = new java.sql.date(date6.gettime());
    }

 /*
    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);
        string format = sdf.format(date);
        system.out.println(format);
        //解析:格式化的逆过程,字符串--->日期
        string str = "21-8-9 下午3:17";
        date date1 = sdf.parse(str);
        system.out.println(date1);
        //******************按照指定的方式格式化和解析:调用带参的构造器*****************************
//        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);//2021-08-09 04:02 13
        //解析:要求字符串必须是符合simpledateformat识别的格式(通过构造器参数体现),否则,报异常
        date date2 = sdf1.parse("2019-08-09 04:02 13");
        system.out.println(date2);
    }
 /*
    练习一:字符串"2020-09-08"转换为java.sql.date
     */
    @test
    public void testexer() throws parseexception {
        string brith = "2020-09-08";
        simpledateformat sdf1 = new simpledateformat("yyy-mm-dd");
        date date = sdf1.parse(brith);
        java.sql.date brithdate = new java.sql.date(date.gettime());
        system.out.println(brithdate);
    }

 /*
    calendar日历类(抽象类)的使用
     */
    @test
    public void testcalendar(){
        //1.实例化
        //方式一:创建其子类(gregoriancalendar)的对象
        //方式二:调用其静态方法getinstance()
        calendar calendar = calendar.getinstance();
        //2.常用方法
        //get()
        int days = calendar.get(calendar.day_of_month);
        system.out.println(days);
        system.out.println(calendar.get(calendar.day_of_year));
        //set()
        calendar.set(calendar.day_of_month,22);
        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 date1 = calendar.gettime();
        calendar.settime(date1);
        days = calendar.get(calendar.day_of_month);
        system.out.println(days);
    }

jdk 8中关于日期和时间的api测试

localdate 、 localtime 、 localdatetime 类是其中较重要的几个类,它们的实例是不可变的对象,分别表示使用 iso —8601日历系统的日期、时间、日期和时间。它们提供了简单的本地日期或时间,并不包含当前的时间信息,也不包含与时区相关的信息。
> localdate 代表 ios 格式( yyyy – mm – dd )的日期,可以存储生日、纪念日等日期。> localtime 表示一个时间,而不是日期。
> localdatetime 是用来表示日期和时间的,这是一个最常用的类之一。
注: iso —8601日历系统是围际标准化组织制定的现代公民的门期和时间的表示法,也就是公历。

/*
    localdate,localtime,localdatetime的使用
     */
    public void test1(){
        //now():获取当前的日期,时间,日期+时间
        localdate localdate = localdate.now();
        localtime localtime = localtime.now();
        localdatetime localdatetime = localdatetime.now();
        system.out.println(localdate);//2021-08-09
        system.out.println(localtime);//21:19:25.264
        system.out.println(localdatetime);//2021-08-09t21:19:25.264
        //of():设置指定的年,月,日,时,分,秒.没有偏移量
        localdatetime localdatetime1 = localdatetime.of(2020,10,6,13,23,43);
        system.out.println(localdatetime1);
        //getxxx():获取相关的属性
        system.out.println(localdatetime.getdayofmonth());
        system.out.println(localdatetime.getdayofweek());
        system.out.println(localdatetime.getmonth());
        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(localdate);
        system.out.println(localdatetime2);
    }

/*
    instant的使用
    类似于java.util.date类
     */
    @test
    public void test2(){
        //now():获取本初子午线对应的标准时间
        instant instant = instant.now();
        system.out.println(instant);//2021-08-09t15:08:46.818z
        //添加时间的偏移量
        offsetdatetime offsetdatetime = instant.atoffset(zoneoffset.ofhours(8));
        system.out.println(offsetdatetime);//2021-08-09t23:08:46.818+08:00
        //toepochmilli():获取自1970年1月1日0时0分0秒(utc)开始的毫秒数-->date类的gettime()
        long milli = instant.toepochmilli();
        system.out.println(milli);//1628521726818
        //ofepochmilli():通过给定的毫秒数,获取instant实例-->date(long millis)
        instant instant1 = instant.ofepochmilli(12243455253l);
        system.out.println(instant1);//1970-05-22t16:57:35.253z
    }
  /*
    datetimeformatter:格式化或解析日期\时间
    类似于simpledateformat
     */
    @test
    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);//2021-08-09t23:43:52.820
        system.out.println(str1);//2021-08-09t23:43:52.82
        //解析:字符串-->日期
//        temporalaccessor parse = formatter.parse(" 2021-08-09t23:43:52.82");
//        system.out.println(parse);
//        方式二:本地化相关的格式,如:oflocalizeddatetime
//        formatstyle.long / formatstyle.medium / formatstyle.short:适用于localdatetime
        datetimeformatter formatter1 = datetimeformatter.oflocalizeddatetime(formatstyle.long);
        //格式化:
        string str2 = formatter1.format(localdatetime);
        system.out.println(str2);//2021年8月10日 上午10时32分48秒
//        本地化相关的格式,如: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);//2021-8-10
 
 
//        重点!!!!!!!:方式三:自定义的格式.如: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);//2021-08-10 10:53:40
        //解析
        temporalaccessor accessor = formatter3.parse("2021-08-10 10:53:40");
        system.out.println(accessor);//{minuteofhour=53, nanoofsecond=0, microofsecond=0, hourofampm=10, secondofminute=40, milliofsecond=0},iso resolved to 2021-08-10
    }

到此这篇关于详解java关于jdk中时间日期的api的文章就介绍到这了,更多相关java api内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!