日期与时间

1.8之前:

        //1.原来创建时间
        Date date = new Date(2022,8,6);
        //2.时间格式化
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //3.将时间转换成字符串
        String format = simpleDateFormat.format(date);
        //4.将字符串转换成时间
        Date parse = simpleDateFormat.parse("2022-03-05");

 

1.8

在日期进行修改的时候,原来的LocalDate对象是不会被修改,每次操作都是返回一个新的LocalDate对象,所以在多线程的场下是线程安全的

        //LocalDate:表示日期,包含年月日  格式为2022-01-01
        //LocalTime:表示时间,包含时分秒  格式为16:39:50.123963852
        //LocalDateTime:表示日期和时间,包含年月日时分秒 格式为2022-01-01T16:33:36.521
        //DateTimeFormatter:日期和时间格式化类
        //Instant:时间戳,表示一个特定的时间瞬间
        //Duration:用于计算2个时间(LocalTime,时分秒)的距离
        //Period:用于计算2个日期(LocalDate,年月日)的距离
        //ZonedDateTime:包含时区的时间

        //ThaiBuddhistDate:泰国佛教日历
        //MinguoDate:中华民国日历
        //JapaneseDate:日本日历
        //HijrahDate:伊斯兰日历
        //1.创建指定的日期
        LocalDate of = LocalDate.of(2022, 03, 04);

        //创建当前的日期
        LocalDate now = LocalDate.now();
        now.getYear();//
        now.getMonth();//
        now.getMonth().getValue();

        now.getDayOfMonth();//
        now.getDayOfWeek();//星期
        now.getDayOfWeek().getValue();

        //2.时间
        // 得到指定的时间
        LocalTime of1 = LocalTime.of(5, 33, 35, 2356);

        //获取当前时间
        LocalTime now1 = LocalTime.now();

        //获取时间信息
        now1.getHour();
        now1.getMinute();
        now1.getSecond();
        now1.getNano();

        //3.日期 和 时间
        //获取指定的日期时间
        LocalDateTime of2 = LocalDateTime.of(2022, 06, 01, 10, 12, 30, 234);
        of2.getYear();
        of2.getMonth();

        //获取当前的日期和时间
        LocalDateTime now2 = LocalDateTime.now();
        //日期时间的修改
        LocalDateTime now = LocalDateTime.now();

        //修改 年
        LocalDateTime localDateTime = now.withYear(1999);

        //修改 月
        LocalDateTime localDateTime1 = now.withMonth(3);

        //修改天
        LocalDateTime localDateTime2 = now.withDayOfMonth(6);

        //修改 小时
        LocalDateTime localDateTime3 = now.withHour(1);

        //修改 分钟
        LocalDateTime localDateTime4 = now.withMinute(16);


        //在当前时间的基础上   加减指定的时间
        now.plusDays(6); //加6天
        now.plusYears(10);//加10年
        now.plusMonths(3);//加3个月

        now.minusYears(10);//减10年
        now.minusMonths(6);//减6个月
        now.minusDays(9);//减9天


        //时间的比较
        LocalDateTime of = LocalDateTime.of(2022,1,6);
        boolean after = now.isAfter(of);
        boolean before = now.isBefore(of);
        boolean equal = now.isEqual(of);
        //1.//格式化 与 解析 操作
        LocalDateTime now = LocalDateTime.now();

        // 使用原来 指定格式
        DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

        //将日期转换 为字符串
        String format = now.format(isoLocalDateTime);

        //2. 自定义格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format1 = now.format(dateTimeFormatter);

        //3.将字符串 解析成 时间
        LocalDateTime parse = LocalDateTime.parse("1999-02-03", dateTimeFormatter);
        //工具类 Instant 又称时间戳   保存了: 1970年1月1日 00:00:00
        Instant now = Instant.now();
        //获取1970年到现在的 纳秒 数据
        int nano = now.getNano();
        //计算日期时间差
        //Duration:用于计算2个时间(LocalTime,时分秒)的距离
        //Period:用于计算2个日期(LocalDate,年月日)的距离
        //时间差
        LocalTime now = LocalTime.now();
        LocalTime of = LocalTime.of(2021, 22, 49);

        Duration between = Duration.between(of, now);
        long l = between.toDays();
        long l1 = between.toHours();
        long l2 = between.toMinutes();
        long l3 = between.toMillis();

        //日期差

 

 
posted @ 2022-09-07 08:18  向大海  阅读(500)  评论(0编辑  收藏  举报