android两个时间比对工具类

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 短信列表时间判断类
 * Created by xing on 2016/7/9.
 */
public class TimeUtil {

    /**
     * 获取系统时间
     * @return String 类型的系统时间
     */
    public static String getSystemTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        String str = formatter.format(curDate);
        return str;
    }

    /**
     * 短信列表时间判断  判断类型为年月日
     * @param time
     * @return
     */
    public static String CompareYear (String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        String str = formatter.format(curDate);
        //W截取年
        time.substring(0,4);
        str.substring(0,4);
        if (!time.substring(0,4).equals(str.substring(0,4))){
            return time;
        } else {
            //月
            time.substring(5,8);
            str.substring(5,8);
            if (! time.substring(5,8).equals(str.substring(5,8))) {
                return time.substring(5,7)+"月"+time.substring(8,10)+"日";
            } else {
                //日
                time.substring(8,11);
                str.substring(8,11);
                if (!time.substring(8,11).equals(str.substring(8,11))) {
                    int a = Integer.parseInt(str.substring(8,10));
                    int b = Integer.parseInt(time.substring(8,10));
                    int c = a -b;
                    if (c <= 6) {
                        Calendar calendar = Calendar.getInstance();//获得一个日历
                        calendar.add(calendar.DAY_OF_WEEK, +0);
                        int number = calendar.get(Calendar.DAY_OF_WEEK);//星期表示1-7,是从星期日开始,
                        String[] strs = {"", "周日", "周一", "周二", "周三", "周四", "周五", "周六",};
                        if (number == 1) {
                            number = 8;
                        }
                        return strs[number -c];
                    }
                    return time.substring(8,10)+"日";
                } else {
                    return time.substring(11,17);
                }
            }
        }
    }

    /**
     * 短信列表时间判断  判断类型为月日
     * @param time
     * @return
     */
    public static String CompareMouth (String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm");
        Date curDate = new Date(System.currentTimeMillis());
        String str = formatter.format(curDate);
        if (time.substring(0,2).equals(str.substring(0,2))) {
            int a = Integer.parseInt(str.substring(3, 5));
            int b = Integer.parseInt(time.substring(3, 5));
            int c = a - b;
            if (c <= 6) {
                Calendar calendar = Calendar.getInstance();//获得一个日历
                calendar.add(calendar.DAY_OF_WEEK, +0);
                int number = calendar.get(Calendar.DAY_OF_WEEK);//星期表示1-7,是从星期日开始,
                String[] strs = {"", "周日", "周一", "周二", "周三", "周四", "周五", "周六",};
                if (number == 1) {
                    number = 8;
                }
                return strs[number - c]+ time.substring(5);
            }
        }
        return time.substring(0,2)+"月"+time.substring(3,5)+"日"+time.substring(5);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值