当前位置: 首页 > 工具软件 > mini calendar > 使用案例 >

【Java】Calendar获取年、月、日、时间

孔厉刚
2023-12-01
转自:https://www.cnblogs.com/longYou/p/6382399.html
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));    //获取东八区时间

        //获取年
        int year = c.get(Calendar.YEAR);
        //获取月份,0表示1月份
        int month = c.get(Calendar.MONTH) + 1;
        //获取当前天数
        int day = c.get(Calendar.DAY_OF_MONTH);
        //获取本月最小天数
        int first = c.getActualMinimum(Calendar.DAY_OF_MONTH);
        //获取本月最大天数
        int last = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        //获取当前小时
        int time = c.get(Calendar.HOUR_OF_DAY);
        //获取当前分钟
        int min = c.get(Calendar.MINUTE);
        //获取当前秒
        int sec = c.get(Calendar.SECOND);                                                     

        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String curDate = s.format(c.getTime());                                      //当前日期
        System.out.println("当前时间:" + year + "-" + month + "-" + day + " " + time + ":" + min + ":" + sec);
        System.out.println("第一天和最后天:" + first +"," + last);
        System.out.println("当前日期:" + curDate);

 类似资料: