Java日期处理

前言

在项目中常常会使用到日期处理的方法,花费了一点时间整理下我使用过的方法,整合成工具类,方便之后使用。java

工具类

package cc.vvxtoys.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 
 * @author vvxtoys
 * @date 2018年1月6日下午9:48:02
 *
 */
public class DateUtils {
	
	private static  Calendar cal = Calendar.getInstance();
	private static String fm = "";//format month
	private static String fd = "";//format day
	
	/**
	 * @description 判断是否为闰年
	 */
	public static boolean isLeapYear(int year){
		return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
	}
	/**
	 * 
	 * @description 获取月天数
	 */
    public static String getMonthDays(int year,int month){
    	String [] days = {"31", null, "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"};
    	if(isLeapYear(year)){
    		days[1]="29";
    	}else{
    		days[1]="28";
    	}
    	return days[month-1];
    } 
    /**
     * @description 获取时间在当前年为第几周
     */
 	public static int getWeekOfYear(String date) throws ParseException {
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");		
 		cal.setFirstDayOfWeek(Calendar.MONDAY);
 		String firstDay = date.substring(0, 4) + "-01-01";
 		cal.setTime(sdf.parse(firstDay));
 		int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
 		if (dayOfWeek == 2) {
 			cal.setTime(sdf.parse(date));
 			int num = cal.get(Calendar.WEEK_OF_YEAR);
 			return num;
 		} else {
 			cal.setTime(sdf.parse(date));
 			int num = cal.get(Calendar.WEEK_OF_YEAR) - 1;
 			return num;
 		}
 	}
 	/**
 	 * 
 	 * @description 天计算
 	 */
 	public static String addDays(String date,int num){
 		try{
 		cal.setTime(toDate(date));
 		cal.add(Calendar.DATE, num);
 		toFormatDate();
 		}catch(Exception e){
 			e.printStackTrace();
 		}
 		return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
 	}	
 	/**
 	 * 
 	 * @description 星期
 	 */
 	public static String addWeeks(String date,int num){
 		try {
			cal.setTime(toDate(date));
			cal.add(Calendar.WEEK_OF_YEAR, num);
			toFormatDate();
		} catch (ParseException e) {
			e.printStackTrace();
		}
 		return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
 	}
 	/**
 	 * 
 	 * @description 月
 	 */
 	public static String addMonths(String date,int num){
 		try {
			cal.setTime(toDate(date));
			cal.add(Calendar.MONTH, num);
			toFormatDate();
		} catch (ParseException e) {
			e.printStackTrace();
		}
 		return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
 	}	
 	/**
 	 * 
 	 * @description 季度
 	 */
 	public static String addQuarters(String date,int num){
 		try {
			cal.setTime(toDate(date));
			cal.add(Calendar.MONTH, num*3);
			toFormatDate();
		} catch (ParseException e) {
			e.printStackTrace();
		}
 		return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
 	}	
 	/**
 	 * 
 	 * @description 年
 	 */
 	public static String addYears(String date,int num){
 		try {
			cal.setTime(toDate(date));
			cal.add(Calendar.YEAR, num);
			toFormatDate();
		} catch (ParseException e) {
			e.printStackTrace();
		}
 		return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
 	}
 	/**
 	 * 
 	 * @description 日期转换
 	 */
 	public static Object translateDate(Object time, String pattern) {
		Object date = new Object();
		if (isEmpty(pattern)) {
			pattern = "yyyy-MM-dd";
		}
		if (isEmpty(time)) {
			time = "1970-1-1";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			if (time instanceof String) {
				date = sdf.parse((String) time);
			} else if (time instanceof Date) {
				date = sdf.format(time);
			} else {
				throw new Exception("转换失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}
 	/**
 	 * 
 	 * @description 当前日期为本周第几天
 	 */
 	public static int getDayOfWeek(String date){
 		try{
 		cal.setTime(toDate(date));
 		cal.setFirstDayOfWeek(Calendar.MONDAY);
 		}catch(ParseException e){
 			e.printStackTrace();
 		}
 		return cal.get(Calendar.DAY_OF_WEEK)==1?7:cal.get(Calendar.DAY_OF_WEEK)-1;
 	}
 	/**
 	 * 
 	 * @description 周一
 	 */
 	public static String getStartDayOfThisWeek(String date){
 	  return addDays(date, -(getDayOfWeek(date)-1));
 	}	
 	/**
 	 * 
 	 * @description 周日
 	 */
 	public static String getEndDayOfThisWeek(String date){
 		return addDays(date,(7-getDayOfWeek(date)));
 	}
 	//获取当前时间
 	public static String getCurrentDate(){
 		Date date = new Date();
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 		return sdf.format(date);
 	}
 	//当前时间
 	public static String getCurrentDate(String pattern){
 		Date date = new Date();
 		if(isEmpty(pattern)){
 			pattern = "yyyy-MM-dd";
 		}
 		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
 		return sdf.format(date);
 	}	
 	/**
 	 * 
 	 * @description 时间间隔  月 季度 年  *3 *12
 	 */
 	public static int getSubDays(int num){
 		Date date = new Date();
 		cal.setTime(date);
 		cal.add(Calendar.MONTH, num);
 		long time = cal.getTimeInMillis() - date.getTime();
 		return (int) (time / (1000 * 60 * 60 * 24));
 	}
 	/**
 	 * 
 	 * @description 获取第几周开始时间
 	 */
 	public static String getStartDayOfWeek(int year,int num){
 		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.WEEK_OF_YEAR, num);
		toFormatDate();
		return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
 	}
 	/**
 	 * 
 	 * @description 获取第几周结束时间
 	 */
 	public static String getEndDayOfWeek(int year,int num){
 		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.WEEK_OF_YEAR, num);
		cal.add(Calendar.DAY_OF_WEEK, 6);
		toFormatDate();
		return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
 	}
 	/**
 	 * 
 	 * @description 获取第几月开始时间
 	 */
 	public static String getStartDayOfMonth(int year,int num){
 		String month = num < 10 ? "0" + num : String.valueOf(num);
		return year + "-" + month + "-" + "01";
 	}
 	/**
 	 * 
 	 * @description 获取第几月结束时间
 	 */
 	public static String getEndDayOfMonth(int year,int num){
 		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, num - 1);
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DAY_OF_YEAR, -1);
		toFormatDate();
		return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
 	}
 	//获取第几季度开始时间
 	public static String getStartDayOfQuarter(int year,int num){
 		num = 3 * num - 2;
		String month = num < 10 ? "0" + num : String.valueOf(num);
		return year + "-" + month + "-" + "01";
 	}
 	//获取第几季度结束时间
 	public static String getEndDayOfQuarter(int year,int num){
 		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, 3 * num - 1);
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DAY_OF_YEAR, -1);
		toFormatDate();
		return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
 	}
 	//时间间隔
 	public static int intervalDay(String d1,String d2){
 		int num = 0;
 		try{
 		long second = toDate(d2).getTime()-toDate(d1).getTime();
 		num=(int)(Math.abs(second)/(1000 * 60 * 60 * 24));
 		}catch(ParseException e){
 			e.printStackTrace();
 		}
 		return num;
 	}
 	private static void toFormatDate(){
 		int month = (cal.get(Calendar.MONTH) + 1);
 		fm = month<10?"0"+month:String.valueOf(month);
 		int day = cal.get(Calendar.DAY_OF_MONTH);
 		fd = day<10?"0"+day:String.valueOf(day);	
 	} 	
 	private static Date toDate(String date) throws ParseException{
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 		return sdf.parse(date);
 	}
 	private static boolean isEmpty(Object obj){
 		return obj==null||obj.toString().trim().equals("");
 	}
}
相关文章
相关标签/搜索