u010441701的专栏

Android 开发

Calendar 显示是否是 今天 本周 本月 本年 (应用于微信 QQ 显示历史聊天列表 时间显示)
/**
     * 是否是昨天
     * 
     * @param time
     * @return
     */
    public static boolean isYesterday(long time) {
        Calendar cal = Calendar.getInstance();
        int d = cal.get(Calendar.DAY_OF_MONTH);
        int dy = cal.get(Calendar.YEAR);
        cal.setTimeInMillis(time);
        int i = cal.get(Calendar.DAY_OF_MONTH);
        int iy = cal.get(Calendar.YEAR);
        if (d == 1) {
            cal.set(Calendar.DAY_OF_MONTH, 0);
            d = cal.get(Calendar.DAY_OF_MONTH);
            return d == i && dy == iy;
        }
        return d - 1 == i && dy == iy;
    }

    /**
     * 得到本周周一
     * 
     */
    public static long getMondayOfThisWeek() {
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 1);
        return c.getTimeInMillis();
    }

    /**
     * 得到本周周日
     * 
     */
    public static long getSundayOfThisWeek() {
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 7);
        return c.getTimeInMillis();
    }

    /**
     * 是否是这个星期
     * 
     * @param time
     * @return
     */
    public static boolean isWeek(long time) {
        long startWeek = getMondayOfThisWeek(); // 本周一
        long endWeek = getSundayOfThisWeek();
        return time >= startWeek && time <= endWeek;
    }

    /**
     * 显示星期格式
     * 
     * @param time
     * @return
     */
    public static String showWeekString(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        if (Calendar.MONDAY == calendar.get(Calendar.DAY_OF_WEEK)) {
            return "星期一";
        }
        if (Calendar.TUESDAY == calendar.get(Calendar.DAY_OF_WEEK)) {
            return "星期二";
        }
        if (Calendar.WEDNESDAY == calendar.get(Calendar.DAY_OF_WEEK)) {
            return "星期三";
        }
        if (Calendar.THURSDAY == calendar.get(Calendar.DAY_OF_WEEK)) {
            return "星期四";
        }
        if (Calendar.FRIDAY == calendar.get(Calendar.DAY_OF_WEEK)) {
            return "星期五";
        }
        if (Calendar.SATURDAY == calendar.get(Calendar.DAY_OF_WEEK)) {
            return "星期六";
        }
        if (Calendar.SUNDAY == calendar.get(Calendar.DAY_OF_WEEK)) {
            return "星期日";
        }
        return "星期一";
    }

    /**
     * 是否是今年
     * 
     * @param time
     * @return
     */
    public static boolean isYear(long time) {
        Calendar cal = Calendar.getInstance();
        int dy = cal.get(Calendar.YEAR);
        cal.setTimeInMillis(time);
        int iy = cal.get(Calendar.YEAR);
        return dy == iy;
    }

    /**
     * 今天 昨天 本周 本年 更久 显示格式
     * 
     * @param time
     * @return
     */
    public static String CastsendTime(long time) {
        // 今天
        if (isCurrentDay(time)) {
            return long2String(time, FORMAT_DATE_HHMM);
        }// 昨天
        else if (isYesterday(time)) {
            return FORMAT_DATE_YESTERDAY;
        }// 这个星期
        else if (isWeek(time)) {
            return showWeekString(time);
        } else if (isYear(time)) {
            return long2String(time, FORMAT_DATE_MMDD);
        } else {
            return long2String(time, FORMAT_DATE_YYMMDD);
        }
    }


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Calendar 显示是否是 今天 本周 本月 本年 (应用于微信 QQ 显示历史聊天列表 时间显示)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭