【Android】判断当天是否属于月份日历的第一行 (仅判断当月,不判断上一个月的日期)

代码:


    /**
     * 判断当天是否属于月份日历的第一行 (仅判断当月,不判断上一个月的日期)
     * @param timeMills
     * @return
     */
    public static boolean isBelongFirstLineOfCalendar(long timeMills){
        boolean isFirstLine = false;
        Date tempDate = new Date(timeMills);
        Calendar c = Calendar.getInstance();
        c.setTime(tempDate);
        //获取该月1号
        Date firstDate = getFirstDayOfMonth(tempDate);
        int firstWeek = getDayOfWeekInt(firstDate.getTime());//周一:0
        //获取第一行起始和结束的日期
        int lastMonthSize = firstWeek;//第一行上个月的日期个数
        int thisMonthSize = 7 - firstWeek;
        if(thisMonthSize < 0){
            return false;
        }
        String firstStr = getYear(firstDate)+"-"+getMonth(firstDate)+"-"+getDay(firstDate)+" "+"00:00:00";
        long firstDayTime = formatStrToDate("yyyy-MM-dd HH:mm:ss",firstStr).getTime();
        long startDayTime = firstDayTime - 24*3600*1000L * lastMonthSize;
        long endDayTime = firstDayTime + 24*3600*1000L * (thisMonthSize );
        if(startDayTime < 0){
            return false;
        }
        if(timeMills >= startDayTime && timeMills <= endDayTime){
            isFirstLine = true;
        }

        return isFirstLine;
    }


    //-------date获取-年月日----------
    public static int getYear(Date date) {
        int year = 0;
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            year = c.get(Calendar.YEAR);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return year;
    }

    public static int getMonth(Date date) {
        int month = 0;
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            month = c.get(Calendar.MONTH) + 1;//函数返回0~11,结果要+1
        } catch (Exception e) {
            e.printStackTrace();
        }
        return month;
    }

    public static int getDay(Date date) {
        int day = 0;
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            day = c.get(Calendar.DAY_OF_MONTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return day;
    }



    /**
     * 返回指定日期的月的第一天
     *
     * @return
     */
    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH), 1);
        return calendar.getTime();
    }


    public static int[] WEEKS4 = {6, 0, 1, 2, 3, 4, 5};
    /**
     * 获取当前星期几 (0~6)
     *
     * @param currentMills 当前时间戳
     * @return int
     */
    public static int getDayOfWeekInt(long currentMills) {
        int res = 0;
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(new Date(currentMills));
            int week = c.get(Calendar.DAY_OF_WEEK);//Calendar.SUNDAY = 1

            res = WEEKS4[week - 1];

        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }


     /**
     * 格式化日期
     *
     * @param pattern 格式 ,例如:yyyy-MM-dd HH:mm:ss
     * @param dateStr
     * @return
     */
    public static Date formatStrToDate(String pattern, String dateStr) {
        Date date = new Date();
        try {
            if (dateStr != null) {
                SimpleDateFormat format = new SimpleDateFormat(pattern);
                date = format.parse(dateStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }




    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值