获取当前日期时间,前一天时间,后一天时间,时间日期对比前后

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

/**
 * 时间(日期)工具
 * @author lyf
 * @date 2015-06-23
 * @version 0.1
 */
public class DateUtil {
	
	Calendar calendar;
	
	/**
	 * 获取currentDate时间的dayNum后(前)的时间
	 * @param currentDate 当前时间,"yyyy-MM-dd HH:mm:ss"
	 * @param dayNum 前(后)几天,负数代表前几天,整数代表后几天
	 * @return 改变后的时间("yyyy-MM-dd HH:mm:ss")
	 */
	public String getBeforeCurrentDate(String currentDate,int dayNum){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (calendar == null) {
			calendar = Calendar.getInstance();
		}
		try {
			Date curDate = formatter.parse(currentDate);
			calendar.setTime(curDate);
			calendar.add(Calendar.DAY_OF_MONTH, dayNum);
			Date bDate = calendar.getTime();
			String beforeDate = formatter.format(bDate);
			return beforeDate;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 获取currentDate时间的dayNum后(前)的时间
	 * @param currentDate 当前时间,格式自定义
	 * @param dayNum 前(后)几天,负数代表前几天,整数代表后几天
	 * @param DateFormat 时间格式
	 * @return 改变后的时间
	 */
	public String getBeforeCurrentDate(String currentDate,int dayNum,String DateFormat){
		SimpleDateFormat formatter = new SimpleDateFormat(DateFormat);
		if (calendar == null) {
			calendar = Calendar.getInstance();
		}
		try {
			Date curDate = formatter.parse(currentDate);
			calendar.setTime(curDate);
			calendar.add(Calendar.DAY_OF_MONTH, dayNum);
			Date bDate = calendar.getTime();
			String beforeDate = formatter.format(bDate);
			return beforeDate;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 比较两个时间先后
	 * @param aTime 格式例如“2015-05-25 17:49:30”
	 * @param bTime 格式例如“2015-05-25 17:49:30”
	 * @return true: aTime在bTime后
	 */
	public static boolean compareTime(String aTime, String bTime) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date aDate = formatter.parse(aTime);
			Date bDate = formatter.parse(bTime);
			if (aDate.after(bDate)) {
				return true;
			}else {
				return false;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 获得当天日期 默认格式为2014-12-05
	 */
	public String getCurrentDate() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(new Date());
	}

	/**
	 * 获得当天日期
	 * @param 格式为type----"yyyyMMddhhmmss"
	 */
	public String getCurrentDate(String type) {
		SimpleDateFormat formatter = new SimpleDateFormat(type);
		return formatter.format(new Date());
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值