获取时间Calendar类(LocalDateTime、LocalDate、LocalTime)

1.Calendar 是一个抽象类,并且构造器是private
2.我们只能通过getInstance()来获取实例
3.里面包含了大量的字段和方法提供给程序员
4. Calendar 没有提供对应的格式化的类,因此需要程序员自己组合来输出(灵活)
5.如果我们想要使用24小时制 Calendar.Hour ==改成==> Calendar.HOUR_OF_DAY

Calendar calendar = Calendar.getInstance();
        System.out.println("获取字段信息"+calendar);
        System.out.println("year:"+calendar.get(Calendar.YEAR));
        //Month获取日历的时候,是按照0开始编号的,因此需要自行加1
        System.out.println("Month:"+(calendar.get(Calendar.MONTH)+1));
        System.out.println("Date:"+calendar.get(Calendar.DATE));
//        System.out.println("Date:"+calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println("12h-Hour:"+ calendar.get(Calendar.HOUR));
        System.out.println("24h-Hour"+calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println("Minute:"+calendar.get(Calendar.MINUTE));
        System.out.println("Second:"+calendar.get(Calendar.SECOND));

        // Calendar 没有提供对应的格式化的类,因此需要程序员自己组合来输出(灵活)
        System.out.println("北京时间:"+calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)
                        +"-"+calendar.get(Calendar.DATE)+"\t"+calendar.get(Calendar.HOUR)+":"+
                        calendar.get(Calendar.MINUTE)+":"+ calendar.get(Calendar.SECOND)
                );

获取时间Calendar类(LocalDateTime、LocalDate、LocalTime)_第1张图片

LocalDateTime、LocalDate、LocalTime:

  System.out.println("=======第三代日期类表示法=======");
        LocalDateTime ldt =  LocalDateTime.now();
        System.out.println(ldt);
        System.out.println("年"+ldt.getYear());
        System.out.println("月:"+ldt.getMonthValue());
        System.out.println("日:"+ldt.getDayOfMonth());
        System.out.println("时"+ldt.getHour());
        System.out.println("分"+ldt.getMinute());
        System.out.println("秒"+ldt.getSecond());

        System.out.println("========LocalDate=======");
        //LocalDate只能获取年月日
        LocalDate lod = LocalDate.now();
        System.out.print("年:"+lod.getYear());
        System.out.print("   月:"+lod.getMonthValue());
        System.out.print("   日:"+lod.getDayOfMonth());

        System.out.println("========LocalTime=======");
        //LocalTime只能获取时分秒
        LocalTime lot = LocalTime.now();
        System.out.print("时:"+lot.getHour());
        System.out.print("   分:"+lot.getMinute());
        System.out.print("   秒:"+lot.getSecond());

获取时间Calendar类(LocalDateTime、LocalDate、LocalTime)_第2张图片

你可能感兴趣的:(韩顺平java习题,java,算法,前端)