zoukankan      html  css  js  c++  java
  • JAVA获取一个月有多少天,以及判断当前天为周几

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    
    /***
     * 
     * @author zhanchaohan
     *
     */
    public class Calender {
        static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        private static void getMonthLastDay(Date date) throws ParseException {
            GregorianCalendar cal = new GregorianCalendar();
    
            cal.setTime(date);
            System.out.println(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        }
    
        private static void getDayOfWeek(Date date) {
            GregorianCalendar cal = new GregorianCalendar();
    
            cal.setTime(date);
            int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
            char[] weekChar = new char[] { '日', '一', '二', '三', '四', '五', '六' };
            System.out.println(weekChar[dayOfWeek - 1]);
        }
    
        public static void main(String[] args) throws ParseException {
    //        getMonthLastDay(sdf.parse("2021-6-1 00:00:00"));
    //        getMonthLastDay(sdf.parse("2021-7-1 00:00:00"));
    //        getMonthLastDay(sdf.parse("2021-8-1 00:00:00"));
    //        getMonthLastDay(sdf.parse("2021-9-1 00:00:00"));
    
            getDayOfWeek(sdf.parse("2021-2-1 00:00:00"));
            getDayOfWeek(sdf.parse("2021-2-2 00:00:00"));
            getDayOfWeek(sdf.parse("2021-2-3 00:00:00"));
            getDayOfWeek(sdf.parse("2021-2-4 00:00:00"));
            getDayOfWeek(sdf.parse("2021-2-5 00:00:00"));
            getDayOfWeek(sdf.parse("2021-2-6 00:00:00"));
            getDayOfWeek(sdf.parse("2021-2-7 00:00:00"));
        }
    }
  • 相关阅读:
    7、shell函数
    5、shell分支
    6、shell循环
    4、shell中的test命令
    3、shell中引号
    2、shell变量
    1、建立和运行shell
    awk命令简介
    18、异步IO
    Python模块:sys
  • 原文地址:https://www.cnblogs.com/zhanchaohan/p/14957472.html
Copyright © 2011-2022 走看看