Java-Date-API

JDK8.0之前日期时间API
* currentTimeMillis():返回当前时间与1970年1月1日0时0分0秒之间一毫秒为单位的时间差(时间戳)
* java.util.Date类
* java.sql.Date类
* SimpleDateFormat:对日期Date类的格式化和解析
* Calendar日历类(抽象类)的使用
public class DateTest {

    @Test
    public void test1()
    {
        long l = System.currentTimeMillis();
        System.out.println(l);
    }

    /**
     * java.util.Date类
     *      java.sql.Date类
     *
     * 1.两个构造器使用
     *  Date():创建一个对应当前时间的Date对象
     *  Date(long date):创建指定毫秒数的Date对象
     * 2.两个方法的使用
     * toString():显示当前年、月、日、时、分、秒
     * getTime():获取当前Date对象对应的毫秒数(时间戳)
     *
     * 3.java.sql.Date对应着数据库中的日期类型的变量
     * >如何实例化
     * >sal.Date  ---->    util.Date对象
     */
    @Test
    public void test2()
    {
        //
        Date date = new Date();
        System.out.println(date);

        System.out.println(date.getTime());

        Date date1=new Date(1596944520022L);
        System.out.println(date1);


        //创建java.sql.Date对象
        java.sql.Date date2=new java.sql.Date(1596944520022L);
        //2020-08-09
        System.out.println(date2);

        //sal.Date  ---->    util.Date对象
        java.sql.Date date3=new java.sql.Date(1596234520022L);
        long time = date3.getTime();
        Date date4=new Date(time);
        System.out.println();
        System.out.println(date3);
        System.out.println(date4);

    }

    /**
     * SimpleDateFormat(String pattern)
     * 1、两个操作
     * ①格式化:日期---->字符串
     * ②解析:字符串 --->日期
     */
    @Test
    public void test3()
    {
        //格式化:使用无参构造器,尽量不用
        Date date=new Date();
//        SimpleDateFormat date1=new SimpleDateFormat();
//        System.out.println(date);
//        String str=date1.format(date);
//        System.out.println(str);
//
//
//        //解析
//        String str2="2020/8/9 下午2:43";
//        try {
//            Date date2 = date1.parse(str2);
//            System.out.println(date2);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }

        //开发中常用:按照指定的方式格式化和解析:调用带参构造器

        //格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format1=sdf.format(date);
        System.out.println(format1);

        //解析
        String str2="2020-02-16 18:00:00";
        try {
            System.out.println(sdf.parse(str2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * Calendar日历类(抽象类)的使用
     *
     *
     *
     */
    @Test
    public void test4()
    {
        //实例化
        /**
         * 方式一:创建其子类GregorianCalendar的对象
         * 方式二:调用其静态方法Calendar.getInstance()
         *
         */

        //GregorianCalendar gc=new GregorianCalendar();

        Calendar calendar=Calendar.getInstance();
        //System.out.println(calendar.getClass());
        /**
         * 常用方法
         * get()
         * set()
         * add()
         * getTime()
         * setTime()
         */

        //get()

        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));

        //Calendar是可变性的
        //set()
        calendar.set(Calendar.DAY_OF_WEEK,2);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));

        //add()
        calendar.add(Calendar.DAY_OF_WEEK,3);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));

        //getTime()
        Date date = calendar.getTime();
        System.out.println(date);


        //setTime()
        Date date1 = new Date();
        calendar.setTime(date1);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
    }
}
JDK8之后新的Date---API
LocalDate
LocalTime
LocalDateTime


Instant:瞬时
* 精度:纳米级
* 实例化:now();
* 类似于java.util.Date类


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

public class DateTest2 {


    @Test
    public void test1()
    {
        //2020-08-09

        //错误写法
        //Date date=new Date(2020,8,9);
        //正确写法(偏移量写法)
        //Date年份从1900年开始,月份从0月开始
        Date date=new Date(2020-1900,8-1,9);
        System.out.println(date);



    }

    /**
     * LocalDate
     * LocalTime
     * LocalDateTime
     */
    @Test
    public void test2()
    {
        //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(2020, 8, 9, 20, 26, 4);
        System.out.println(localDateTime1);


        //get相关

        System.out.println(localDateTime1.getDayOfMonth());
        System.out.println(localDateTime1.getDayOfWeek());
        System.out.println(localDateTime1.getMonth());

        //withXXX();设置相关的属性
        //体现了不可变性
        LocalDateTime localDateTime2 = localDateTime1.withDayOfMonth(22);
        System.out.println(localDateTime1);
        System.out.println(localDateTime2);
        LocalDateTime localDateTime3 = localDateTime1.withHour(22);
        System.out.println(localDateTime3);

        //plusXXX();加
        LocalDateTime localDateTime4 = localDateTime1.plusHours(3);
        System.out.println(localDateTime4);

        //minusXXX();减
        LocalDateTime localDateTime5 = localDateTime1.minusHours(2);
        System.out.println(localDateTime5);


    }

    /**
     * Instant:瞬时
     * 精度:纳米级
     * 实例化:now();
     * 类似于java.util.Date类
     */
    //时间戳

    @Test
    public void test3()
    {
        //按零时区计算
        Instant instant=Instant.now();
        System.out.println(instant);

        //添加时间的偏移量
        //东八区
        OffsetDateTime instant1=instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(instant1);

        //获取对应毫秒数
//        Long l2=System.currentTimeMillis();
//        System.out.println(l2);
        //toEpochMilli():获取自1970年1月1日0时0分0秒开始的毫秒数  ---->Date.getTime();
        long l = instant.toEpochMilli();

        System.out.println(l);


        //ofEpochMilli(Long millis);通过给定的毫秒数,获取Instant实例  -->Date(Long millis)
        Instant instant2 = Instant.ofEpochMilli(1596978103787L);
        System.out.println(instant2);



    }


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

    @Test
    public void test4()
    {
        //方式一:预定义的标准格式。如ISO_LOCAL_DATE_TIME、ISO_LOCAL_DATE、ISO_LOCAL_TIME
        DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期-->字符串
        LocalDateTime localDateTime=LocalDateTime.now();
        String str1=isoLocalDateTime.format(localDateTime);
        System.out.println(localDateTime);
        System.out.println(str1);
        //解析:字符串-->日期
        TemporalAccessor parse=isoLocalDateTime.parse("2020-08-09T21:53:17.207289100");
        System.out.println(parse);


        //方式二:本地化相关的格式
        //ofLocalizedDateTime(FormatStyle.SHORT)
        //ofLocalizedDateTime(FormatStyle.LONG)
        //ofLocalizedDateTime(FormatStyle.MEDIUM)
        LocalDateTime localDateTime1=LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        //DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.systemDefault());

        //格式化
        String str2=formatter.format(localDateTime1);
        System.out.println(str2);


        //重点:方式三、自定义的格式。 如:ofPattern("yyyy-MM-dd hh:mm:ss E")
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");

        //格式化
        String s3=formatter1.format(LocalDateTime.now());
        System.out.println(s3);

        //解析
        String s4="2020-08-09 10:17:25";
        TemporalAccessor parse1 = formatter1.parse(s4);
        System.out.println(parse1);

    }
}
public class DateTest2 {


    @Test
    public void test1()
    {
        //2020-08-09

        //错误写法
        //Date date=new Date(2020,8,9);
        //正确写法(偏移量写法)
        //Date年份从1900年开始,月份从0月开始
        Date date=new Date(2020-1900,8-1,9);
        System.out.println(date);



    }

    /**
     * LocalDate
     * LocalTime
     * LocalDateTime
     */
    @Test
    public void test2()
    {
        //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(2020, 8, 9, 20, 26, 4);
        System.out.println(localDateTime1);


        //get相关

        System.out.println(localDateTime1.getDayOfMonth());
        System.out.println(localDateTime1.getDayOfWeek());
        System.out.println(localDateTime1.getMonth());

        //withXXX();设置相关的属性
        //体现了不可变性
        LocalDateTime localDateTime2 = localDateTime1.withDayOfMonth(22);
        System.out.println(localDateTime1);
        System.out.println(localDateTime2);
        LocalDateTime localDateTime3 = localDateTime1.withHour(22);
        System.out.println(localDateTime3);

        //plusXXX();加
        LocalDateTime localDateTime4 = localDateTime1.plusHours(3);
        System.out.println(localDateTime4);

        //minusXXX();减
        LocalDateTime localDateTime5 = localDateTime1.minusHours(2);
        System.out.println(localDateTime5);


    }

    /**
     * Instant:瞬时
     * 精度:纳米级
     * 实例化:now();
     * 类似于java.util.Date类
     */
    //时间戳

    @Test
    public void test3()
    {
        //按零时区计算
        Instant instant=Instant.now();
        System.out.println(instant);

        //添加时间的偏移量
        //东八区
        OffsetDateTime instant1=instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(instant1);

        //获取对应毫秒数
//        Long l2=System.currentTimeMillis();
//        System.out.println(l2);
        //toEpochMilli():获取自1970年1月1日0时0分0秒开始的毫秒数  ---->Date.getTime();
        long l = instant.toEpochMilli();

        System.out.println(l);


        //ofEpochMilli(Long millis);通过给定的毫秒数,获取Instant实例  -->Date(Long millis)
        Instant instant2 = Instant.ofEpochMilli(1596978103787L);
        System.out.println(instant2);



    }


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

    @Test
    public void test4()
    {
        //方式一:预定义的标准格式。如ISO_LOCAL_DATE_TIME、ISO_LOCAL_DATE、ISO_LOCAL_TIME
        DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期-->字符串
        LocalDateTime localDateTime=LocalDateTime.now();
        String str1=isoLocalDateTime.format(localDateTime);
        System.out.println(localDateTime);
        System.out.println(str1);
        //解析:字符串-->日期
        TemporalAccessor parse=isoLocalDateTime.parse("2020-08-09T21:53:17.207289100");
        System.out.println(parse);


        //方式二:本地化相关的格式
        //ofLocalizedDateTime(FormatStyle.SHORT)
        //ofLocalizedDateTime(FormatStyle.LONG)
        //ofLocalizedDateTime(FormatStyle.MEDIUM)
        LocalDateTime localDateTime1=LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        //DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.systemDefault());

        //格式化
        String str2=formatter.format(localDateTime1);
        System.out.println(str2);


        //重点:方式三、自定义的格式。 如:ofPattern("yyyy-MM-dd hh:mm:ss E")
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");

        //格式化
        String s3=formatter1.format(LocalDateTime.now());
        System.out.println(s3);

        //解析
        String s4="2020-08-09 10:17:25";
        TemporalAccessor parse1 = formatter1.parse(s4);
        System.out.println(parse1);

    }
}

 

 
posted @ 2020-08-09 23:27  orz江小鱼  阅读(138)  评论(0编辑  收藏  举报