Android 日期计算类

分享一个使用较方便的日期计算类:

package com.utils.datecount;

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

public class DateCount {

	/**
	 * datelevel 0为7天内,1为7到15天,2为15天以上
	 */
	public static int datelevel = 0;
	public static int positionweek = -1;
	public static int position2week = -1;
	
	/**
	 * 获取与今天时间差
	 * @param endTime
	 * @return 
	 */
	public static double countDate(String startTime) {
		
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// 输入日期的格式
		Date date = null;
			try {
				date = simpleDateFormat.parse(startTime);
				if (date == null) return 0;
			} catch (java.text.ParseException e) {
				return 0;
//				e.printStackTrace();
			}
		GregorianCalendar cal1 = new GregorianCalendar();
		GregorianCalendar cal2 = new GregorianCalendar();
		cal1.setTime(date);
		cal2.setTime(curDate);
		double dayCount = (cal2.getTimeInMillis() - cal1.getTimeInMillis()) / (1000 * 3600 * 24);// 从间隔毫秒变成间隔天数
		return dayCount;
	}
}

package com.utils.tools;

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

import android.text.TextUtils;
import android.text.format.DateFormat;

/**
 * 日期格式化工具类
 * 
 * @author Harryxu
 * 
 */
public class DateUtil {
	
	public static Date convert2Date(String format, String dateStr) {
		Date date = null;
		try {
			if (TextUtils.isEmpty(format)) format = "yyyy-MM-dd HH:mm:ss";
			return new java.sql.Date(new SimpleDateFormat(format).parse(dateStr).getTime());
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	public static CharSequence formatDate(String format, String dateStr) {
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (date == null) date = new Date();
		if (TextUtils.isEmpty(format)) format = "yyyy-MM-dd";
		return DateFormat.format(format, date);
	}

	/**
	 * 根据偏量值取得传入时间的前后天数
	 * 
	 * @param date 
	 * @param offset 时间偏量值
	 * @return
	 */
	public static Date getNextPreDay(Date date, int offset) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, offset);
		date = calendar.getTime();
		return date;
	}
	
	public static int getDays(Date date1, Date date2) {
		Date kaishi = date1, jieshu = date2;
		if (date1.compareTo(date2) > 0) {
			kaishi = date2;
			jieshu = date1;
		} 
		
		long diff = jieshu.getTime() - kaishi.getTime();
		float days = diff / 24f / 60 / 60 / 1000;
		return (int) ((days - (int) days) >= 0 ? (days + 1) : days);
	}
	
	public static String addMinites( String time, int addminite){
		SimpleDateFormat myFormatter = new SimpleDateFormat("HH:mm");   
		Calendar totime = Calendar.getInstance();
		java.util.Date date = null;
		try {
			date = myFormatter.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (date == null) return null;
		totime.setTime(date);
		totime.add(Calendar.MINUTE, addminite);
		date = totime.getTime();
		return myFormatter.format(date);
		
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值