java date 工具类

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

public class DateUtil {
	
	public final static String startTime = " 00:00:00";
	public final static String endTime = " 23:59:59";
	public final static String yyyyMMdd_0 = "yyyyMMdd";
	public final static String yyyyMMdd_1 = "yyyy-MM-dd";
	public final static String yyyyMMdd_2 = "yyyy年MM月dd日";
	public final static String yyyyMMdd_3 = "yyyy/MM/dd";
	public final static String yyyyMMdd_4 = "yyyyMMddHHmmss";
	public final static String yyyyMMdd_5 = "yyyy-MM-dd HH:mm:ss";
	public final static String yyyyMMdd_6 = "yyyy年MM月dd日  HH时mm分ss秒";
	public final static String yyyyMMdd_7 = "yyyy/MM/dd HH:mm:ss";
	public final static String yyyy_0 = "yyyy";
	public final static String yyyy_1 = "yyyy年";
	public final static String yyyyMM_0 = "yyyyMM";
	public final static String yyyyMM_1 = "yyyy-MM";
	public final static String yyyyMM_2 = "yyyy年MM月";
	public final static String MMdd_0 = "MMdd";
	public final static String MMdd_1 = "MM-dd";
	public final static String MMdd_2 = "MM月dd日";
	public final static String MMdd_3 = "MM/dd";
	public final static String MM_0 = "MM";
	public final static String MM_1 = "MM月";
	public final static String DD_0 = "dd";
	public final static String DD_1 = "dd日";
	
	/**
	 * 获取当前日期
	 * @param dateFormat 日期格式
	 * @return
	 */
	public static String currentDate(String dateFormat){
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);//设置日期格式
		return df.format(new Date());
	} 
	/**
	 * 获取当前日期+n天的日期
	 * @param dateFormat 日期格式
	 * @param n 减去的天数
	 * @return
	 */
	public static String currentDate_N(String dateFormat,int n){
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);//设置日期格式
		Calendar c = Calendar.getInstance();  
        c.setTime(new Date());  
        c.add(Calendar.DAY_OF_MONTH, n);
        return df.format(c.getTime());
	} 
	/**
	 * 获取当前月+n个月的当前日期
	 * @param dateFormat 日期格式
	 * @param n
	 * @return
	 */
	public static String currentMonth_N(String dateFormat,int n){
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);//设置日期格式
		Calendar c = Calendar.getInstance();  
        c.add(Calendar.MONTH, n);
        return df.format(c.getTime());
	} 
	/**
	 * 获取当前月+n个月的第1天的日期
	 * @param dateFormat
	 * @param n
	 * @param d
	 * @return
	 */
	public static String currentMonth_fDate(String dateFormat,int n){
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);//设置日期格式
		Calendar c = Calendar.getInstance();  
		c.add(Calendar.MONTH, n);
		c.set(Calendar.DAY_OF_MONTH,1);
		return df.format(c.getTime());
	} 
	/**
	 * 获取当前月+(n-1)个月的最后一天的日期
	 * @param dateFormat
	 * @param n
	 * @param d
	 * @return
	 */
	public static String currentMonth_LDate(String dateFormat,int n){
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);//设置日期格式
		Calendar c = Calendar.getInstance();  
		c.add(Calendar.MONTH, n);
		c.set(Calendar.DAY_OF_MONTH,0);
		return df.format(c.getTime());
	} 
	/**
	 * 获取第当前年的第一天
	 * @param dateFormat
	 * @param n
	 * @return
	 */
	public static String currYearFirst(String dateFormat){
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);//设置日期格式
		Calendar c = Calendar.getInstance();
		c.add(Calendar.YEAR, 0);
		c.set(Calendar.DAY_OF_YEAR, 1);
		return df.format(c.getTime());
	} 
	/**
	 * 获取第当前年的最后一天
	 * @param dateFormat
	 * @param n
	 * @return
	 */
	public static String currYearLast(String dateFormat){
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);//设置日期格式
		Calendar c = Calendar.getInstance();
		c.add(Calendar.YEAR, 1);
		c.set(Calendar.DAY_OF_YEAR, -1);
		return df.format(c.getTime());
	} 
	/**
	 * 获取本周+n周的第m天
	 * @param 本周+n周的第m天,m<7
	 * @param dateFormat 日期格式
	 * @return
	 */
	public static String AweekToDay(int n,int m,String dateFormat){
		if(m<7){
			SimpleDateFormat df = new SimpleDateFormat(dateFormat);//设置日期格式
			Date date = new Date();
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
			if (dayofweek == 1) {
				dayofweek += 7;
			}
			cal.add(Calendar.DATE, 2 - dayofweek + 7*n+m);
			return df.format(cal.getTime());
		}
		return null;
	}

	
	/**
	 * 获取当前日期为当月的第几周
	 * @return
	 */
	public static int Aweek(){
		Calendar c = Calendar.getInstance();
		// 当前日期是本月第几周
		return c.get(Calendar.WEEK_OF_MONTH);
	}
	/**
	 * 获取当前季度
	 *
	 */
	public static String getQuarter() {
		Calendar c = Calendar.getInstance();
		int month = c.get(c.MONTH) + 1;
		String quarter;
		if (month >= 1 && month <= 3) {
			quarter = "一";
		} else if (month >= 4 && month <= 6) {
			quarter = "二";
		} else if (month >= 7 && month <= 9) {
			quarter = "三";
		} else {
			quarter = "四";
		}
		return quarter ;
	}
	
	
	
	/**
	 * 获取当前季度
	 *
	 */
	public static int getIntQuarter() {
		Calendar c = Calendar.getInstance();
		int month = c.get(c.MONTH) + 1;
		 int quarter = 0;
		if (month >= 1 && month <= 3) {
			quarter = 1;
		} else if (month >= 4 && month <= 6) {
			quarter = 2;
		} else if (month >= 7 && month <= 9) {
			quarter = 3;
		} else {
			quarter = 4;
		}
		return quarter ;
	}

	/**
	 * 获取某季度的第一天和最后一天
	 *	@param num第几季度
	 */
	public static String[] getCurrQuarter(int num,String dateFormat) {
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);
		String[] s = new String[2];
		String str = "";
		// 设置本年的季
		Calendar quarterCalendar = null;
		switch (num) {
			case 1: // 本年到现在经过了一个季度,在加上前4个季度
				quarterCalendar = Calendar.getInstance();
				quarterCalendar.set(Calendar.MONTH, 3);
				quarterCalendar.set(Calendar.DATE, 1);
				quarterCalendar.add(Calendar.DATE, -1);
				str = df.format(quarterCalendar.getTime());
				s[0] = str.substring(0, str.length() - 5) + "01-01";
				s[1] = str;
				break;
			case 2: // 本年到现在经过了二个季度,在加上前三个季度
				quarterCalendar = Calendar.getInstance();
				quarterCalendar.set(Calendar.MONTH, 6);
				quarterCalendar.set(Calendar.DATE, 1);
				quarterCalendar.add(Calendar.DATE, -1);
				str = df.format(quarterCalendar.getTime());
				s[0] = str.substring(0, str.length() - 5) + "04-01";
				s[1] = str;
				break;
			case 3:// 本年到现在经过了三个季度,在加上前二个季度
				quarterCalendar = Calendar.getInstance();
				quarterCalendar.set(Calendar.MONTH, 9);
				quarterCalendar.set(Calendar.DATE, 1);
				quarterCalendar.add(Calendar.DATE, -1);
				str = df.format(quarterCalendar.getTime());
				s[0] = str.substring(0, str.length() - 5) + "07-01";
				s[1] = str;
				break;
			case 4:// 本年到现在经过了四个季度,在加上前一个季度
				quarterCalendar = Calendar.getInstance();
				str = df.format(quarterCalendar.getTime());
				s[0] = str.substring(0, str.length() - 5) + "10-01";
				s[1] = str.substring(0, str.length() - 5) + "12-31";
				break;
		}
		return s;
	}

	
	/**
	 * 获取上个季度的最后一天
	 *	@param num第几季度
	 * @throws ParseException 
	 */
	public static String[] getLastCurrQuarter(String date,String dateFormat) throws ParseException {
		Date date1 = strTodate(date,dateFormat);
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);
		String[] s = new String[2];
		Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
       s[0]=df.format(getFirstDayOfQuarter(calendar.get(Calendar.YEAR),getQuarterOfYear(date1)));
       s[1]=df.format(getLastDayOfQuarter(calendar.get(Calendar.YEAR),getQuarterOfYear(date1))); 
         
        return s;
	}
	
	/**
     * 返回指定年季的季的第一天
     *
     * @param year
     * @param quarter
     * @return
     */
    public static Date getFirstDayOfQuarter(Integer year, Integer quarter) {
        Calendar calendar = Calendar.getInstance();
        Integer month = new Integer(0);
        if (quarter == 1) {
            month = 1 - 1;
        } else if (quarter == 2) {
            month = 4 - 1;
        } else if (quarter == 3) {
            month = 7 - 1;
        } else if (quarter == 4) {
            month = 10 - 1;
        } else {
            month = calendar.get(Calendar.MONTH);
        }
        return getFirstDayOfMonth(year, month);
    }
    /**
     * 返回指定年月的月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getFirstDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        if (month == null) {
            month = calendar.get(Calendar.MONTH);
        }
        calendar.set(year, month, 1);
        return calendar.getTime();
    }
	
	/**
     * 返回指定年季的季的最后一天
     *
     * @param year
     * @param quarter
     * @return
     */
    public static Date getLastDayOfQuarter(Integer year, Integer quarter) {
        Calendar calendar = Calendar.getInstance();
        Integer month = new Integer(0);
        if (quarter == 1) {
            month = 3 - 1;
        } else if (quarter == 2) {
            month = 6 - 1;
        } else if (quarter == 3) {
            month = 9 - 1;
        } else if (quarter == 4) {
            month = 12 - 1;
        } else {
            month = calendar.get(Calendar.MONTH);
        }
        return getLastDayOfMonth(year, month);
    }
    /**
     * 返回指定年月的月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        if (month == null) {
            month = calendar.get(Calendar.MONTH);
        }
        calendar.set(year, month, 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }
    /**
     * 返回指定日期的月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR),
                     calendar.get(Calendar.MONTH), 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }
    
    /**
     * 返回指定日期的上个月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfLastMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR),
                     calendar.get(Calendar.MONTH) - 1, 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }
	
	
	/**
     * 返回指定日期的季度
     *
     * @param date
     * @return
     */
    public static int getQuarterOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) / 3 + 1;
    }	
	
	
	
	
	
	
	/**
     * 日期转星期
     * 
     * @param datetime
     * @return
     */
    public static String dateToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = { "日", "一", "二", "三", "四", "五", "六" };
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
	/**
	 * String 转 Date 
	 * @param datetime 要和 dateFormat长度一致
	 * @param dateFormat
	 * @return
	 * @throws ParseException
	 */
	public static Date strTodate(String datetime,String dateFormat) throws ParseException{
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);
		return df.parse(datetime);
	}
	/**
	 * Date 转  String
	 * @param datetime 要和 dateFormat长度一致
	 * @param dateFormat
	 * @return
	 * @throws ParseException
	 */
	public static String dateTostr(Date date,String dateFormat) throws ParseException{
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);
		return df.format(date);
	}
	
	
	/**
	 * 传入一个字符串日期,返回一个字符串日期
	 * 注意:有效字符的dateFormat长度  <= dateFormat1长度 比如 yyyy-MM-dd的长度等于yyyyMMdd的长度
	 * yyyy-MM-dd 的长度 > yyyyMM 的 长度 
	 * @param datetime  传入的时间(目前传入的时间格式只支持 yyyy-MM-dd 格式的)
	 * @param dateFormat 需要的格式
	 * @param dateFormat1 传入的时间的格式
	 * @return
	 * @throws ParseException
	 */
	public static String strDateFormat(String datetime,String dateFormat,String dateFormat1) throws ParseException{
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);//设置日期格式
		Calendar c = Calendar.getInstance();  
        c.setTime(strTodate(datetime,dateFormat1));  
        c.add(Calendar.DAY_OF_MONTH, 0);
        return df.format(c.getTime());
	}
	
	
	
	
	
	/**
	 * 获取上一年的指定日期
	 * @param datetime
	 * @param dateFormat
	 * @return
	 * @throws ParseException
	 */
	public static String LastYearTime(String datetime,String dateFormat) throws ParseException{
		SimpleDateFormat format = new SimpleDateFormat(dateFormat);
		Calendar c = Calendar.getInstance();
		c.setTime(strTodate(datetime,dateFormat));
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
		return format.format(y);
	}
	/**
	 * 获取一个时间段的所有日期
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static List<String> findDates(String startDate, String endDate,String dateFormat) throws ParseException {
		Date dStart= strTodate(startDate, dateFormat);
		Date dEnd= strTodate(endDate, dateFormat);
		Calendar c = Calendar.getInstance();
        c.setTime(dStart);
        List<String> dateList = new ArrayList<String>();
        //把起始日期加上
        dateList.add(startDate);
        // 此日期是否在指定日期之后
        while (dEnd.after(c.getTime())) {
        // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
            c.add(Calendar.DAY_OF_MONTH, 1);
            dateList.add(dateTostr(c.getTime(),dateFormat));
        }
        return dateList;
	}
	
	public static List<String> findMONTH(String startDate, String endDate,String dateFormat) throws ParseException {
		Date dStart= strTodate(startDate, dateFormat);
		Date dEnd= strTodate(endDate, dateFormat);
		Calendar c = Calendar.getInstance();
        c.setTime(dStart);
        List<String> dateList = new ArrayList<String>();
        //把起始日期加上
        dateList.add(startDate);
        // 此日期是否在指定日期之后
        while (dEnd.after(c.getTime())) {
        // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
            c.add(Calendar.MONTH, 1);
            dateList.add(dateTostr(c.getTime(),dateFormat));
        }
        return dateList;
	}

	
	public static void main(String[] args) throws ParseException {

		List<String> strs = findMONTH("2020-08-01","2020-07-25",yyyyMMdd_1);
		for(String str:strs){
			System.out.println(str);
		}
		
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值