Java常用日期操作总结

一、获得本周的第一天

/**
 * 获得当周的第一天
 * @return
 */
public static Date getWeekFirstDayAsDate() {
	Calendar cal = Calendar.getInstance();
	cal.setTime(new Date());
	cal.set(Calendar.DAY_OF_WEEK, 1);
	cal.set(Calendar.HOUR_OF_DAY, 0);
	cal.set(Calendar.MINUTE, 0);
	cal.set(Calendar.SECOND, 0);
	//西方国家默认从周日开始算,中国从周一开始算,所以往后推1天
	cal.add(Calendar.DAY_OF_YEAR, 1);
	return cal.getTime();
}

二、获得本月的第一天

/**
 * 获得当月的第一天
 * @return
 */
public static Date getMonthFirstDayAsDate() {
	//格式化类
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
	Calendar cal = Calendar.getInstance();
	cal.setTime(new Date());
	cal.set(Calendar.DAY_OF_MONTH, 1);
	cal.set(Calendar.HOUR_OF_DAY, 0);
	cal.set(Calendar.MINUTE, 0);
	cal.set(Calendar.SECOND, 0);
	return cal.getTime();
}

三、获得两个日期间的所有日期

/**
 * 描述:获得两个日期间的所有日期
 * @param beginDate
 * @param endDate
 * @return
 */
public static List getBetweenDaysAsDate(Date beginDate,Date endDate) {
	//日期格式化类
	List list = new ArrayList<>();

	//设置起始日期
	Calendar begin = Calendar.getInstance();
	begin.setTime(beginDate);
	
	//设置结束日期
	Calendar end = Calendar.getInstance();
	end.setTime(endDate);
	
	while(begin.getTimeInMillis()<=end.getTimeInMillis()) {
		list.add(begin.getTime());
		//日期后退一天
		begin.add(Calendar.DAY_OF_YEAR, 1);
	}
	return list;
}

四、将日期转成为完成格式

/**
 * 描述: 把日期转成完整格式。如:2014-1-1 转化后为 2014-01-01
 * 
 * @param strDate
 * @return
 */
public static String toComplexDate(String strDate) {
	try {
		String tmp_date[] = strDate.split("-");
		String tmp_year = tmp_date[0];
		String tmp_month = tmp_date[1];
		String tmp_day = tmp_date[2];
		if (Integer.parseInt(tmp_month) <= 9) {
			tmp_month = "0" + tmp_month;
		}
		if (Integer.parseInt(tmp_day) <= 9) {
			tmp_day = "0" + tmp_day;
		}
		return tmp_year + "-" + tmp_month + "-" + tmp_day;
	} catch (Exception er) {
		return strDate;
	}
}

五、获得N天后的数据

/**
 * 描述:获得N天后的日期
 * @param date 指定日期
 * 
 * @param days 指定N天
 * 
 * @return
 */
public static Date getAfterDateByDays(Date date, int days) {
	try {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.add(Calendar.DAY_OF_YEAR, days);
		return now.getTime();
	} catch (Exception er) {
		return date;
	}
}

六、计算两日期间的相差天数

/**
 * 描述: 计算两日期相差天数
 * 
 * @param startDate
 *            开始日期
 * @param endDate
 *            结束日期
 * @return
 */
public static int getBetweenDays(Date startDate, Date endDate) {
	try {
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(startDate);
		long startTime = startCalendar.getTimeInMillis();
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		long endTime = endCalendar.getTimeInMillis();
		long between_days = (endTime - startTime) / (1000 * 60 * 60 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	} catch (Exception er) {
		return 0;
	}
}

七、获得某一天的开始时间

/**
 * 描述: 获取某一天的开始时间
 * 
 * @param date
 *            日期
 * @return
 */
public static String getDayStart(Date date) {
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);
	calendar.set(Calendar.HOUR_OF_DAY, 0);
	calendar.set(Calendar.MINUTE, 0);
	calendar.set(Calendar.SECOND, 0);
	Date zero = calendar.getTime();
	return df.format(zero);
}

八、获得某一天的结束时间

/**
 * 描述: 获取某一天的开始时间
 * 
 * @param date
 *            日期
 * @return
 */
public static String getDayStart(Date date) {
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);
	calendar.set(Calendar.HOUR_OF_DAY, 0);
	calendar.set(Calendar.MINUTE, 0);
	calendar.set(Calendar.SECOND, 0);
	Date zero = calendar.getTime();
	return df.format(zero);
}

 

你可能感兴趣的:(Java常用日期操作总结)