Java的常用类之二 ----日期时间类

1、jdk8 之前的日期时间AP测试

1.system类中的currentTimeMillis()
2.java.util.Date 和 Java.sql.Date
3.SimpleDateFormat类
4.Calendar类

1.1、currentTimeMillis():返回当前的计算机时间,时间的表达格式为当前计算机的时间和GMT时间(格林威治时间),获取1970年1月1日0时0分0秒(UTC)开始的毫秒数
1.2、字符串2019-09-08转换为java.sql.Date
    public void testExer() throws ParseException {
        String brith =  "2019-09-08";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf1.parse(brith);
        System.out.println(date);

        java.sql.Date brithDate =   new java.sql.Date(date.getTime());
        System.out.println(brithDate);
    }
1.3、 SimpleDateFormat的使用 :SimpleDateFormat类对日期的格式化解析

① 两个操作
1.1格式化:日期 —>字符串
1.2解析:字符串—>日期

②SimpleDateFormat的实例化

public void test() throws ParseException {
        //实例化
        SimpleDateFormat sdf = new SimpleDateFormat();
        //格式化:日期  --->字符串
        Date date =new Date();
        System.out.println(date);

        String  format = sdf.format(date);
        System.out.println(format);

//        解析:字符串--->日期

        String str = "20-3-25 下午10:02";
        Date date1 = sdf.parse(str);
        System.out.println(date1);
        System.out.println("**********************************");

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = sdf1.format(date);
        System.out.println(format1);
    }
1.4、Calendar日历类(抽象类)的使用

①实例化
//方式一:创建子类的(GregorianCalendar)对象
//方式二:调用其静态方法getInstance();

  Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getClass());

② 常用方法

  //get() 获取当前日期的年月日
        int days = calendar.get(calendar.DAY_OF_MONTH);
        System.out.println(days);
        //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);
        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);
2、jdk8的日期时间的api测试
2.1、 LocalDate、LocalTime、LocalDateTime的使用 本地的日期时间

说明:
1.LocalDateTime相较于LocalDate、LocalTime使用频率较高
2.类似于Calender

    public void testDate(){//已过期 deprecated  偏移量的问题
        Date date = new Date(2020 - 1900,9 - 1,8);
        System.out.println(date);
    }
@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(2020,10,26);
//        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());

        //有返回值,不用修改原来的值,体现不可变性,修改值
        //with():设置相关属性
        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(22);
        System.out.println(localDateTime1);

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

        //不可变性,plus添加相关时间
        LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
        System.out.println(localDateTime);
        System.out.println(localDateTime3);

        //原来的值不变,新增新的值  minus  减去相应的时间
        LocalDateTime localDateTime4 = localDateTime.minusDays(6);
        System.out.println(localDateTime);
        System.out.println(localDateTime4);
    }
2.2、Instant类:时间上的一个瞬时点,这可能被用来记录应用程序中的事件时间戳。

1.在Java中,从1970年1月1日0时0分0秒开始,以毫秒为单位。
2.类似于java.util.Date类

 @Test
    public void  test1(){
        //now():获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);

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

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

        //通过给定的毫秒数,获取Instant实例 -->date类中的getTime()
        Instant instant1 = Instant.ofEpochMilli(1585193930875L);
        System.out.println(instant1);

    }
2.3、DateTimeFormatter:格式化或解析日期、时间类似于simpleDateFormat

方式一:预定义标准格式

  /*
        三种方式
        1.ISO_LOCAL_DATE_TIME
        2.ISO_LOCAL_DATE
        3.ISO_LOCAL_TIME
         */
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期--->字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String str = formatter.format(localDateTime);
        System.out.println(localDateTime);
        System.out.println(str);//20-3-26 下午8:05

        //解析:字符串-->日期
        TemporalAccessor parse = formatter.parse("2020-03-26T19:58:57.398");
        System.out.println(parse);

方式二:本地化相关的格式: 如ofLocalizedDateTime(),FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT:适用于LocalDateTime

		  DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
//        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
//        DateTimeFormatter formatter3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        //格式化
        String str2 = formatter1.format(localDateTime);
        System.out.println(str2);

        //本地化相关方法 如:ofLocalizedDate()
        // FormatStyle.FULL/ FormatStyle.LONG / FormatStyle.MEDIUM /FormatStyle.SHORT:适用于LocalDate
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String format = formatter2.format(LocalDate.now());
        System.out.println(format);//2020年3月26日 星期四

重点:方法三:自定义的格式 如:ofPattern("yyyy-MM-dd hh:mm:ss ")

 DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String format1 = formatter3.format(LocalDateTime.now());
        System.out.println(format1);
        //解析
        TemporalAccessor parse1 = formatter3.parse("2019-03-26 08:14:19");
        System.out.println(parse1);
Java的常用类之一 ---- String、StringBuffer、StringBuilder类

https://blog.csdn.net/weixin_43244120/article/details/105187804

Java的常用类之三----其他类(接口)

https://blog.csdn.net/weixin_43244120/article/details/105189069

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值