Java基础:时间类的使用

时间类

Date类

        /* System.currentTimeMillis():返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。称为时间戳 */
        System.out.println(System.currentTimeMillis());

        /* Date类 */
            //java.util.Date
        Date utildate = new Date();
        Date utildate2 = new Date(utildate.getTime());
            //java.sql.Date
        Date sqldate = new java.sql.Date(utildate.getTime());
            //toString() 、 getTime()
        System.out.println(utildate);//Wed May 19 18:27:40 CST 2021
        System.out.println(utildate2);//Wed May 19 18:28:46 CST 2021
        System.out.println(sqldate);//2021-05-19
        System.out.println(sqldate.getTime());

        //如何将java.util.Date对象转换为java.sql.Date对象
        //情况一:多态性声明为util的date4,创建的是sql.Date的对象,然后可以强转为sql.Date
        Date date4 = new java.sql.Date(2343243242323L);
        java.sql.Date date5 = (java.sql.Date) date4;
        //情况二:如果创建是util的对象,则无法强转
        Date date6 = new Date();
        java.sql.Date date7 = new java.sql.Date(date6.getTime());

simpleDateFormat类

/* SimpleDateFormate的使用:*/
        //创建simpleDateFormat对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        //格式化
        String format = simpleDateFormat.format(new Date().getTime());//21-5-19 下午7:02
        //解析
        Date parse = simpleDateFormat.parse(format);
        /* 指定方式的构造器 */
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format1 = simpleDateFormat2.format(new Date().getTime());
        Date parse1 = simpleDateFormat2.parse(format1);

Calendar日历类

        /* Calendar日历类 */
        //实例化先
        Calendar instance = Calendar.getInstance();
        //get()方法
        int i = instance.get(Calendar.DAY_OF_MONTH);//这个月的第几天
        int i1 = instance.get(Calendar.DAY_OF_YEAR);//这年的第几天
        
        //set()
        instance.set(Calendar.DAY_OF_MONTH,22);

        //add()
        instance.add(Calendar.DAY_OF_MONTH,3);
        
        //getTime()
        Date time = instance.getTime();

        //setTime()
        Date date = new Date();
        instance.setTime(date);

⭐新增时间类java.time

        /* java.time.*内的方法 */
        //LocalDate
        LocalDate now = LocalDate.now();
        System.out.println(now);//年-月-日
        LocalDate of = LocalDate.of(2020, 10, 4);
        System.out.println(of);//2020-10-4

        //LocalTime
        LocalTime localTime = LocalTime.now();
        LocalTime of1 = localTime.of(12, 42, 24);

        //LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime of2 = LocalDateTime.of(2020, 12, 3, 23, 42, 52);

        //getXxx()
        LocalDateTime.now().getMonth();//英文月份
        localDateTime.getYear();
        localDateTime.getDayOfMonth();
        localDateTime.getDayOfWeek();
        localDateTime.getDayOfYear();
        localDateTime.getHour();

        //with
        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(20);//将日期改为这个月的第20天返回字符串

        //plus
        LocalDateTime localDateTime2 = localDateTime.plusWeeks(3);//加三周返回字符串
        
        //minus
        LocalDateTime localDateTime3 = localDateTime.plusDays(3);//减三天返回字符串

instant类

        /* instant类 */
        Instant instant = Instant.now();
        System.out.println(instant);//本初子午线的时间

        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));//中国的时间东8区?

        long now = instant.toEpochMilli();//时间戳
        Instant instant1 = instant.ofEpochMilli(491949419194L);//返回一个时间戳的时间

⭐dateTimeFormatter类

        /* dateTimeFormatter:格式化或解析日期、时间 */
        //实例化方法 1
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        LocalDateTime localDateTime = LocalDateTime.now();

        String format = dateTimeFormatter.format(localDateTime);
        System.out.println(format);

        TemporalAccessor parse = dateTimeFormatter.parse(format);
        System.out.println(parse);

        //方式二
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        //FormatStyle.SHORT:形式不同,表示不同
        LocalDateTime localDateTime1 = LocalDateTime.now();

        String format1 = dateTimeFormatter1.format(localDateTime1);
        System.out.println(format1);
        TemporalAccessor parse1 = dateTimeFormatter1.parse(format1);
        System.out.println(parse1);


一般使用自定义方式

        //方式三:自定义格式常用
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        String format2 = dateTimeFormatter2.format(LocalDateTime.now());
        System.out.println(format2);

        TemporalAccessor parse2 = dateTimeFormatter2.parse(format2);
        System.out.println(parse2);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值