javaweb开发中常用的时间工具类

package com.bx.util.commonUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 时间日期的工具类
 * @author Administrator
 *
 */
public class Date_util {

	/**
	 * 获取给定匹配格式的format对象
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static SimpleDateFormat getFormat(String pattern) throws Exception{ 
		
		return new SimpleDateFormat(pattern);
	}
	/**
	 * 按给定格式格式化给定日期,返回格式化日期字符串
	 * @param format
	 * @param date
	 * @return
	 */
	public static String getFormatDateStr(SimpleDateFormat format,Date date){
		
		return format.format(date);
	}
	/**
	 * 根据传入字段做对应的加减
	 * @param date
	 * @param amount
	 * @param field:year、month、date、hour、minute、second
	 * 
	 * @return
	 */
	public static Date addByField(Date date,int amount,int field){
		
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setTime(date);
		calendar.add(field, amount);
		return calendar.getTime();
	}
	/**
	 * 获取传入格式的当前时间
	 * @param pattern
	 * @return
	 * @throws Exception 
	 */
	public static String getFormatCurrentTime(String pattern) throws Exception{
		
		SimpleDateFormat format = getFormat(pattern);
		return format.format(new Date());
	}
	/**
	 * 获取格式化的前一天的日期
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static String getFormatYesterday(String pattern) throws Exception{
		
		SimpleDateFormat format = getFormat(pattern);
		return format.format(addByField(new Date(), -1, Calendar.DATE));
	}
	
	/**
	 * 获取指定格式化的明天的时间
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static String getFormatTomorrow(String pattern) throws Exception{
		
		SimpleDateFormat format = getFormat(pattern);
		return format.format(addByField(new Date(), 1, Calendar.DATE));
	}
	/**
	 * 获得给定日期所在周的周一的时间,返回格式化日期字符串
	 * @param date
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static String getFirstDayOfWeek(Date date,String pattern) throws Exception{
		
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);//按国内的习惯指定一周的第一天为周一
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
		return getFormat(pattern).format(calendar.getTime());
	}
	/**
	 * 获得给定日期所在周的周日时间,返回格式化字符串
	 * @param date
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static String getLastDayOfWeek(Date date,String pattern) throws Exception{
		
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);//按国内的习惯指定一周的第一天为周一
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()+6);
		return getFormat(pattern).format(calendar.getTime());
	}
	/**
	 * 获得指定日期所在月的第一天日期,返回格式化字符串
	 * @param date
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static String getFirstDayofMonth(Date date,String pattern) throws Exception{
		
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return getFormat(pattern).format(calendar.getTime());
	}
	/**
	 * 获得指定日期所在月最后一天的日期,返回格式化字符串,
	 * 此方法利用日历类的封装可以免去对某一个月具体天数的讨论
	 * @param date
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static String getLastDayofMonth(Date date,String pattern)throws Exception{
		
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return getFormat(pattern).format(calendar.getTime());
		
	}
	/**
	 * 获取给定时间段内每一天的格式化日期列表
	 * @param startDate
	 * @param endDate
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static List getDateList(Date startDate,Date endDate,String pattern) throws Exception{
		
		List list = new ArrayList();
		SimpleDateFormat format = getFormat(pattern);
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(startDate);
		while(startDate.compareTo(endDate)<0){
			list.add(format.format(calendar.getTime()));
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			startDate = calendar.getTime(); 
		}
		return list;
	}
	/**
	 *返回两个日期之间的天数(注意该方法传入的参数如果精确到时分秒的话,会以24小时为一天进行计算,可能出现预期之外的结果)
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws Exception
	 */
	public static int daysOfTwoDate(Date startDate,Date endDate) throws Exception{
		
		if(startDate == null || endDate == null){
			return -1;
		}
		long startTime = startDate.getTime();
		long endTime = endDate.getTime();
		return (int)((endTime - startTime)/(24*60*60*1000));
	}
	/**
	 * 返回两个日期之间的天数的另一个方法(推荐使用)
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws Exception
	 */
	public static int daysOfTwoCal(Date startDate,Date endDate) throws Exception{
		
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(startDate);
		int startDayOfYear = cal.get(Calendar.DAY_OF_YEAR);
		cal.setTime(endDate);
		int endDayOfYear = cal.get(Calendar.DAY_OF_YEAR);
		return endDayOfYear-startDayOfYear;
	}
}

你可能感兴趣的:(java)