其他分享
首页 > 其他分享> > Calendar类

Calendar类

作者:互联网

Calendar

public static void main(String[] args) {
        //1.创建Calendar对象
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getTime().toLocaleString());
        System.out.println(calendar.getTimeInMillis());
        // 2.获取时间信息
        //获取年
        int year = calendar.get(Calendar.YEAR);

        //月
        int month = calendar.get(Calendar.MONTH);

        //日
        int day = calendar.get(Calendar.DAY_OF_MONTH);//Date

        //小时
        int hour = calendar.get(Calendar.HOUR_OF_DAY);//HOUR 12小时,  HOUR_OF_DAY 24小时

        //分钟
        int minute = calendar.get(Calendar.MINUTE);

        //秒
        int second = calendar.get(Calendar.SECOND);
        System.out.println(year + "年" + (month+1) + "月" + day + "日"
                + hour + ":" + minute + ":" + second);

        //3.修改时间
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.DAY_OF_MONTH, 13);
        System.out.println(calendar2.getTime().toLocaleString());

        //4.add()方法修改时间
        calendar2.add(Calendar.HOUR,1);
        System.out.println(calendar2.getTime().toLocaleString());

        //5.补充方法
        int max = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
        int min = calendar2.getActualMinimum(Calendar.DAY_OF_MONTH);
        System.out.println(max);
        System.out.println(min);

    }

标签:get,int,calendar2,println,Calendar,calendar
来源: https://www.cnblogs.com/wshjyyysys/p/15802197.html