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"));
    }
}

 

posted @ 2021-07-01 09:57  Jachs  阅读(3843)  评论(0编辑  收藏  举报