日期相关

package com.example.salesmanapp.tool;

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

import android.annotation.SuppressLint;
import android.util.Log;

@SuppressLint("SimpleDateFormat") 
public class DateUtils { 
	 private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();
	 /**
	     * 判断是否为今天(效率比较高)
	     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
	     * @return true今天 false不是
	     * @throws ParseException
	     */
	    public static boolean IsToday(String day) throws ParseException {

	        Calendar pre = Calendar.getInstance();
	        Date predate = new Date(System.currentTimeMillis());
	        pre.setTime(predate);

	        Calendar cal = Calendar.getInstance();
	        Date date = getDateFormat().parse(day);
	        cal.setTime(date);

	        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
	            int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR);

	            if (diffDay == 0) {
	                return true;
	            }
	        }
	        return false;
	    }

	    /**
	     * 判断是否为昨天(效率比较高)
	     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
	     * @return true今天 false不是
	     * @throws ParseException
	     */
	    public static boolean IsYesterday(String day) throws ParseException {

	        Calendar pre = Calendar.getInstance();
	        Date predate = new Date(System.currentTimeMillis());
	        pre.setTime(predate);

	        Calendar cal = Calendar.getInstance();
	        Date date = getDateFormat().parse(day);
	        cal.setTime(date);

	        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
	            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
	                    - pre.get(Calendar.DAY_OF_YEAR);

	            if (diffDay == -1) {
	                return true;
	            }
	        }
	        return false;
	    }

	    public static SimpleDateFormat getDateFormat() {
	        if (null == DateLocal.get()) {
	            DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
	        }
	        return DateLocal.get();
	    }

		public static String  GetDateStr(Date date) {
			if (date==null) {
				return null;
			}
			String datestr=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
			try {
				if(IsToday(datestr)){
					datestr="今天"+new SimpleDateFormat("HH:mm").format(date);
				} else if(IsYesterday(datestr)){
					datestr="昨天"+new SimpleDateFormat("HH:mm").format(date);
				}
			} catch (ParseException e) { 
//				e.printStackTrace();
				Log.e("date changeError", e.getMessage());
			} 
			return datestr;
		}
   
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值