Calendar使用--> 获取星期几、一周日期的函数

 // 一、获取星期几:
    private String getWeekDay(Calendar c)
    {
        if (c == null)
        {
            return "星期一";
        }


        if (Calendar.MONDAY == c.get(Calendar.DAY_OF_WEEK))
        {
            return "星期一";
        }
        if (Calendar.TUESDAY == c.get(Calendar.DAY_OF_WEEK))
        {
            return "星期二";
        }
        if (Calendar.WEDNESDAY == c.get(Calendar.DAY_OF_WEEK))
        {
            return "星期三";
        }
        if (Calendar.THURSDAY == c.get(Calendar.DAY_OF_WEEK))
        {
            return "星期四";
        }
        if (Calendar.FRIDAY == c.get(Calendar.DAY_OF_WEEK))
        {
            return "星期五";
        }
        if (Calendar.SATURDAY == c.get(Calendar.DAY_OF_WEEK))
        {
            return "星期六";
        }
        if (Calendar.SUNDAY == c.get(Calendar.DAY_OF_WEEK))
        {
            return "星期日";
        }


        return "星期一";
    }


    // 二、一周日期的函数:通过在本周的星期一的Calendar上连续加1,获取一周的日期
    // (1)获得当前日期与本周一相差的天数
    private int getMondayPlus()
    {
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1)
        {
            return -6;
        }
        else
        {
            return 2 - dayOfWeek;
        }
    }


    // (2) 获得本周星期一的日期
    public Calendar getCurrentMonday()
    {
        int mondayPlus = this.getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        Calendar c = Calendar.getInstance();
        c.setTime(monday);
        return c;
    }


    // (3)通过在本周的星期一的Calendar上连续加1,获取一周的日期
    public ArrayList<WeekInfo> getCurrentWeekInfoList()
    {
        Calendar now = Calendar.getInstance();
        todayInfo = now.get(Calendar.YEAR) + "-" + (now.get(Calendar.MONTH) + 1) + "-"
                + now.get(Calendar.DAY_OF_MONTH);


        ArrayList<WeekInfo> weekInfoList = new ArrayList<WeekInfo>();
        Calendar mondayDate = getCurrentMonday(); // 本周星期一的Calendar
        String monday = (mondayDate.get(Calendar.MONTH) + 1) + "月"
                + mondayDate.get(Calendar.DAY_OF_MONTH) + "日" + " " + "星期一";
        WeekInfo weekInfo = new WeekInfo();
        weekInfo.setTodayInfo(todayInfo);
        weekInfo.setWeekDayInfo(monday);
        weekInfo.setWeekDate(mondayDate.get(Calendar.YEAR) + "-"
                + (mondayDate.get(Calendar.MONTH) + 1) + "-"
                + mondayDate.get(Calendar.DAY_OF_MONTH));
        weekInfoList.add(weekInfo);


        mondayDate.add(Calendar.DAY_OF_MONTH, 1);
        String tuesday = (mondayDate.get(Calendar.MONTH) + 1) + "月"
                + mondayDate.get(Calendar.DAY_OF_MONTH) + "日" + " " + "星期二";
        WeekInfo weekInfo2 = new WeekInfo();
        weekInfo2.setTodayInfo(todayInfo);
        weekInfo2.setWeekDayInfo(tuesday);
        weekInfo2.setWeekDate(mondayDate.get(Calendar.YEAR) + "-"
                + (mondayDate.get(Calendar.MONTH) + 1) + "-"
                + mondayDate.get(Calendar.DAY_OF_MONTH));
        weekInfoList.add(weekInfo2);


        mondayDate.add(Calendar.DAY_OF_MONTH, 1);
        String wednesday = (mondayDate.get(Calendar.MONTH) + 1) + "月"
                + mondayDate.get(Calendar.DAY_OF_MONTH) + "日" + " " + "星期三";
        WeekInfo weekInfo3 = new WeekInfo();
        weekInfo3.setTodayInfo(todayInfo);
        weekInfo3.setWeekDayInfo(wednesday);
        weekInfo3.setWeekDate(mondayDate.get(Calendar.YEAR) + "-"
                + (mondayDate.get(Calendar.MONTH) + 1) + "-"
                + mondayDate.get(Calendar.DAY_OF_MONTH));
        weekInfoList.add(weekInfo3);


        mondayDate.add(Calendar.DAY_OF_MONTH, 1);
        String thursday = (mondayDate.get(Calendar.MONTH) + 1) + "月"
                + mondayDate.get(Calendar.DAY_OF_MONTH) + "日" + " " + "星期四";
        WeekInfo weekInfo4 = new WeekInfo();
        weekInfo4.setTodayInfo(todayInfo);
        weekInfo4.setWeekDayInfo(thursday);
        weekInfo4.setWeekDate(mondayDate.get(Calendar.YEAR) + "-"
                + (mondayDate.get(Calendar.MONTH) + 1) + "-"
                + mondayDate.get(Calendar.DAY_OF_MONTH));
        weekInfoList.add(weekInfo4);


        mondayDate.add(Calendar.DAY_OF_MONTH, 1);
        String friday = (mondayDate.get(Calendar.MONTH) + 1) + "月"
                + mondayDate.get(Calendar.DAY_OF_MONTH) + "日" + " " + "星期五";
        WeekInfo weekInfo5 = new WeekInfo();
        weekInfo5.setTodayInfo(todayInfo);
        weekInfo5.setWeekDayInfo(friday);
        weekInfo5.setWeekDate(mondayDate.get(Calendar.YEAR) + "-"
                + (mondayDate.get(Calendar.MONTH) + 1) + "-"
                + mondayDate.get(Calendar.DAY_OF_MONTH));
        weekInfoList.add(weekInfo5);


        mondayDate.add(Calendar.DAY_OF_MONTH, 1);
        String saturday = (mondayDate.get(Calendar.MONTH) + 1) + "月"
                + mondayDate.get(Calendar.DAY_OF_MONTH) + "日" + " " + "星期六";
        WeekInfo weekInfo6 = new WeekInfo();
        weekInfo6.setTodayInfo(todayInfo);
        weekInfo6.setWeekDayInfo(saturday);
        weekInfo6.setWeekDate(mondayDate.get(Calendar.YEAR) + "-"
                + (mondayDate.get(Calendar.MONTH) + 1) + "-"
                + mondayDate.get(Calendar.DAY_OF_MONTH));
        weekInfoList.add(weekInfo6);


        mondayDate.add(Calendar.DAY_OF_MONTH, 1);
        String sunday = (mondayDate.get(Calendar.MONTH) + 1) + "月"
                + mondayDate.get(Calendar.DAY_OF_MONTH) + "日" + " " + "星期日";
        WeekInfo weekInfo7 = new WeekInfo();
        weekInfo7.setTodayInfo(todayInfo);
        weekInfo7.setWeekDayInfo(sunday);
        weekInfo7.setWeekDate(mondayDate.get(Calendar.YEAR) + "-"
                + (mondayDate.get(Calendar.MONTH) + 1) + "-"
                + mondayDate.get(Calendar.DAY_OF_MONTH));
        weekInfoList.add(weekInfo7);


        return weekInfoList;
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值