日期工具类

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DateUtil {
	
	private DateUtil(){}

	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static final SimpleDateFormat datetimefomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
	private static Map<String, Integer> dayOfWeeks = new HashMap<String, Integer>();
	private static List<String> dayOfWeekList = new ArrayList<String>();
	
	static{
		dayOfWeeks.put("一", Calendar.MONDAY);
		dayOfWeeks.put("二", Calendar.TUESDAY);
		dayOfWeeks.put("三", Calendar.WEDNESDAY);
		dayOfWeeks.put("四", Calendar.THURSDAY);
		dayOfWeeks.put("五", Calendar.FRIDAY);
		dayOfWeeks.put("六", Calendar.SATURDAY);
		dayOfWeeks.put("日", Calendar.SUNDAY);
		
		dayOfWeekList.add("一");
		dayOfWeekList.add("二");
		dayOfWeekList.add("三");
		dayOfWeekList.add("四");
		dayOfWeekList.add("五");
		dayOfWeekList.add("六");
		dayOfWeekList.add("日");
	}
	
	/**
	 * 获取指定日期所的下一周的星期一的日期,格式为yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static String getNextMondayDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 7);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return sdf.format(calendar.getTime());
	}
	
	/**
	 * 获取指定日期的下一周的星期日的日期,格式为yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static String getNextSundayDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 7);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return sdf.format(calendar.getTime());
	}
	
	/**
	 * 计算两个日期之间的分钟数
	 * @param beginDateTime
	 * @param endDateTime
	 * @return
	 */
	public static Long calculateMinutesBetweenDates(Timestamp beginDateTime, Timestamp endDateTime){
		long minutes = (Long.valueOf(endDateTime.getTime() - beginDateTime.getTime()) / 1000 / 60);
		
		return minutes;
	}

	/**
	 * 获取指定日期所在星期的星期一的日期,格式为yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static String getMondayDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return sdf.format(calendar.getTime());
	}
	
	/**
	 * 获取指定日期所在星期的星期四的日期,格式为yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static String getThursdayDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
		return sdf.format(calendar.getTime());
	}
	
	/**
	 * 获取指定日期所在星期的星期日的日期,格式为yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static String getSundayDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return sdf.format(calendar.getTime());
	}
	
	/**
	 * 根据星期获取当前周的日期,格式为yyyy-MM-dd
	 * @param dayOfWeek 星期,例如:一、二、三、四、五、六和日
	 * @return
	 */
	public static String getDateInCurrentWeek(String dayOfWeek){
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTimeInMillis(System.currentTimeMillis());
		calendar.set(Calendar.DAY_OF_WEEK, dayOfWeeks.get(dayOfWeek));
		return sdf.format(calendar.getTime());
	}
	
	/**
	 * 根据一个日期格式的字符串返回一个Date对象
	 * @param date 格式:yyyy-MM-dd HH:mm:ss,如果不是该格式,返回当前时间
	 * @return
	 */
	public static Date parseDate(String date){
		Date result = null;
		if(date == null)
			result = new Date();
		
		try {
			result = datetimefomat.parse(date);
		} catch (ParseException e1) {
			result = new Date();
			System.err.println("occured a error when parse a date, param is: "+date);
		}
		
		return result;
	}
	
	/**
	 * 获取当前时间,格式:HH:mm:ss
	 * @return
	 */
	public static String getCurrentTime(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		return timeFormat.format(calendar.getTime());
	}
	
	/**
	 * 获取指定日期对象所在的年
	 * @param date
	 * @return
	 */
	public static int getYear(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}
	
	/**
	 * 获取指定日期对象所在的月
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return (calendar.get(Calendar.MONTH)+1);
	}
	
	/**
	 * 获取指定日期对象对应的那一天
	 * @param date
	 * @return
	 */
	public static int getDay(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DATE);
	}
	
	/**
	 * 获取中文格式的星期,例如:一、二、三,等等
	 * @param date
	 * @return
	 */
	public static String getCNDayOfWeek(Date date){
		String result = "";
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		for(Map.Entry<String, Integer> entry : dayOfWeeks.entrySet()){
			if(entry.getValue() == dayOfWeek){
				result = entry.getKey();
				break;
			}
		}
		
		return result;
	}
	
	/**
	 * 获取指定日期在当前年中的星期数,就是说指定日期在当前年中是第几周
	 * @param date 如果date为空,返回0
	 * @return
	 */
	public static int getWeekOfYear(Date date){
		if(date == null)
			return 0;
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		
		return calendar.get(Calendar.WEEK_OF_YEAR);
	}
	
	/**
	 * 返回中文格式的星期列表
	 * @return 一、二、三、四、五、六和日
	 */
	public static List<String> getCNDayofWeekList(){
		return dayOfWeekList;
	}
	
	/**
	 * 判断指定的日期是否是星期日
	 * @param date
	 * @return
	 */
	public static boolean isSunday(Date date){
		if(date == null)
			return false;
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		
		return (Calendar.SUNDAY == calendar.get(Calendar.DAY_OF_WEEK));
	}
	
	/**
	 * 判断指定的日期是否是跨年周
	 * @param date
	 * @return
	 */
	public static boolean isSpanYearsWeek(Date date){
		if(date == null)
			return false;
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		int yearForMonday = calendar.get(Calendar.YEAR);
		
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		int yearForSunday = calendar.get(Calendar.YEAR);
		
		return (yearForMonday != yearForSunday);
	}
	
	/**
	 * 获取指定日期所在的年的最大周数
	 * @param date
	 * @return
	 */
	public static int getMaxWeekOfYear(Date date){
		if(date == null)
			return 0;
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.WEEK_OF_YEAR);
	}
}

你可能感兴趣的:(java)