判断两个时间相差多少天多少分多少秒

package com.itaoo.android.huabao.utils;


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


/**
 * 
 * @author Stephen Zhu
 *
 */
public class TimeUtil {

	
	private static final String TAG = TimeUtil.class.getSimpleName();

	/** one day millisecond count*/
	public static final long ONE_DAY_MILLISECONDS = 1000*3600*24;
	
	public static final long ONE_HOUR_MILLISECONDS = 1000*3600;
	
	public static final long ONE_MIN_MILLISECONDS = 1000*60;
	
	/**
	 * format yyyy-MM-dd mm:ss to yyyy年MM月dd日
	 * @param dateStr
	 * @return
	 */
	public static String formatDate(String dateStr){
		String ret = dateStr.split(" ")[0];
		String[] rets = ret.split("-");
		ret = rets[0] + "年" + rets[1] + "月" + rets[2] + "日";
		return ret;
	}
	
	/**
	 * get expired days
	 * @param dateStr
	 * @return -1 -> expired, 0 -> expired after today 
	 */
	public static int getExpiredDays(String dateStr){
		int ret = -1;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		Date date;
		try {
			date = sdf.parse(dateStr);
			Date dateNow = new Date();
			long times = date.getTime() - dateNow.getTime();
			if(times > 0){
				ret = ((int)(times / ONE_DAY_MILLISECONDS));
			}else{
				ret = -1;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

		
		Log.d(TAG, "dateStr = " + dateStr + " days = " + ret);
		return ret;
	}
	
	public static int getExpiredDays(String dateStr, String endTime){
		int ret = -1;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		Date date;
		try {
			date = sdf.parse(dateStr);
			Date dateNow = sdf.parse(endTime);
			long times = date.getTime() - dateNow.getTime();
			if(times > 0){
				ret = ((int)(times / ONE_DAY_MILLISECONDS));
			}else{
				ret = -1;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

		
		Log.d(TAG, "dateStr = " + dateStr + " days = " + ret);
		return ret;
	}
	
	public static int getExpiredHour(String dateStr){
		int ret = -1;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		Date date;
		try {
			date = sdf.parse(dateStr);
			Date dateNow = new Date();
			
			long times = date.getTime() - dateNow.getTime();
			if(times > 0){
				ret = ((int)(times / ONE_HOUR_MILLISECONDS));
			}else{
				ret = -1;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

		
		Log.d(TAG, "dateStr = " + dateStr + " hours = " + ret);
		return ret;
	}
	
	public static int getExpiredHour(String dateStr, String endTime){
		int ret = -1;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		Date date;
		try {
			date = sdf.parse(dateStr);
			Date dateNow = sdf.parse(endTime);
			
			long times = date.getTime() - dateNow.getTime();
			if(times > 0){
				ret = ((int)(times / ONE_HOUR_MILLISECONDS));
			}else{
				ret = -1;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

		
		Log.d(TAG, "dateStr = " + dateStr + " hours = " + ret);
		return ret;
	}
	
	public static int getExpiredMin(String dateStr){
		int ret = -1;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		Date date;
		try {
			date = sdf.parse(dateStr);
			Date dateNow = new Date();
			
			long times = date.getTime() - dateNow.getTime();
			if(times > 0){
				ret = ((int)(times / ONE_MIN_MILLISECONDS));
			}else{
				ret = -1;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Log.d(TAG, "dateStr = " + dateStr + " mins = " + ret);
		return ret;
	}
	
	public static int getExpiredMin(String dateStr, String endTime){
		int ret = -1;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		Date date;
		try {
			date = sdf.parse(dateStr);
			Date dateNow = sdf.parse(endTime);
			
			long times = date.getTime() - dateNow.getTime();
			if(times > 0){
				ret = ((int)(times / ONE_MIN_MILLISECONDS));
			}else{
				ret = -1;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

		
		Log.d(TAG, "dateStr = " + dateStr + " mins = " + ret);
		return ret;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值