DateUtil

package com.cinc.ecmp.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.StringTokenizer;

/**
 * 
 * @author 
 *
 */
public class DateUtil {

	private DateUtil() {
	}

	public static Timestamp getNowTimeStamp() {
		return new Timestamp(System.currentTimeMillis());
	}

	
	/**
	 * 
	 * @param endDate
	 *            ,被减数
	 * @param startDate
	 *            , 减数 计算两个日期之间的相隔天数
	 * @throws ParseException
	 */
	public static long dateCal(String endDate, String startDate)
			throws ParseException {
		String s1 = endDate.replace('-', '/');
		String s2 = startDate.replace('-', '/');
		long days = 0;
		Date dt1 = fomatStd().parse(s1);
		Date dt2 = fomatStd().parse(s2);
		days = (dt1.getTime() - dt2.getTime()) / 60 / 60 / 1000 / 24;
		return days;
	}
	
	/**
	 * 
	 * @param time
	 * 			,时间
	 * @param parm
	 * 			,计算参数
	 * @param type
	 * 			,计算类型
	 * @return
	 * @throws ParseException
	 */
	public static String calTime(String time, int parm, String type)
			throws ParseException {
		Date time1 = formatZkbcTimes().parse(time);
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(time1);
		if ("HH".equals(type)) {
			rightNow.add(Calendar.HOUR,parm); 
		} else if ("mm".equals(type)){
			rightNow.add(Calendar.MINUTE,parm);
		} else if ("ss".equals(type)){
			rightNow.add(Calendar.SECOND,parm);
		}
		Date dt1=rightNow.getTime();
	  	String lastTime = formatZkbcTimes().format(dt1);
		return lastTime;
	}

	/**
	 * 
	 * @param dateTime
	 * 			,日期时间
	 * @param parm
	 * 			,计算参数
	 * @param type
	 * 			,计算类型
	 * @return
	 * @throws ParseException
	 */
	public static String calDateTime(String dateTime, int parm, String type)
			throws ParseException {
		Date dateTime1 = formatDayTimeZkbc().parse(dateTime);
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(dateTime1);
		if ("yyyy".equals(type)){
			rightNow.add(Calendar.YEAR,parm); 
		}
		else if ("MM".equals(type)){
			rightNow.add(Calendar.MONTH,parm);
		}else if ("dd".equals(type)){
			rightNow.add(Calendar.DAY_OF_YEAR,parm);
		}else if ("HH".equals(type)){
			rightNow.add(Calendar.HOUR,parm); 
		}else if ("mm".equals(type)){
			rightNow.add(Calendar.MINUTE,parm);
		}else if ("ss".equals(type)){
			rightNow.add(Calendar.SECOND,parm);
		}
		Date dt1=rightNow.getTime();
	  	String lastDateTime = formatDayTimeZkbc().format(dt1);
		return lastDateTime;
	}
	
	
	/**
	 * 
	 * @param date
	 * 			,日期
	 * @param parm
	 * 			,计算参数
	 * @param type
	 * 			,计算类型
	 * @return
	 * @throws ParseException
	 */
	public static String calDate(String date, int parm, String type)
			throws ParseException {
		Date date1 = fomatStd().parse(date);
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date1);
		if ("yyyy".equals(type)){
			rightNow.add(Calendar.YEAR,parm); 
		}else if ("MM".equals(type)){
			rightNow.add(Calendar.MONTH,parm);
		}else if ("dd".equals(type)){
			rightNow.add(Calendar.DAY_OF_YEAR,parm);
		}
		Date dt1=rightNow.getTime();
	  	String lastDate = fomatStd().format(dt1);
		return lastDate;
	}
	
	
	public static long timeCal(String endTime, String startTime)
			throws ParseException {
		String s1 = endTime.replaceAll("-", "").replaceAll(":", "");
		String s2 = startTime.replaceAll("-", "").replaceAll(":", "");
		long times = 0;
		Date dt1 = formatAll().parse(s1);
		Date dt2 = formatAll().parse(s2);
		times = (dt1.getTime() - dt2.getTime()) / 60 / 1000;
		return times;
	}

	/**
	 * 
	 * @param date1
	 *            ,被减数
	 * @param date2
	 *            , 减数 比较两个日期的大小
	 * @throws ParseException
	 */
	public static int comparingTwoDates(String date1, String date2)
			throws ParseException {
		String s1 = date1.replace('-', '/');
		String s2 = date2.replace('-', '/');
		long days = 0;
		int result = 0;
		Date dt1 = fomatStd().parse(s1);
		Date dt2 = fomatStd().parse(s2);
		days = (dt1.getTime() - dt2.getTime()) / 60 / 60 / 1000 / 24;
		if (days == 0) {
			result = 0;
		} else if (days < 0) {
			result = -1;
		} else if (days > 0) {
			result = 1;
		}
		return result;
	}

	/**
	 * 
	 * @param times1
	 *            ,被减数
	 * @param times2
	 *            , 减数 比较两个时间的大小
	 * @throws ParseException
	 */
	public static int comparingTwoTimes(String times1, String times2) {
		DateFormat df = new SimpleDateFormat("hh:mm:ss");
		try {
			Date d1 = df.parse(times1);
			Date d2 = df.parse(times2);
			if (d1.getTime() > d2.getTime()) {
				return 1;
			} else if (d1.getTime() < d2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 时间比较
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException
	 */
	public static int comparingTwoTimes2(String date1, String date2)
			throws ParseException {
		long days = 0;
		int result = 0;
		Date dt1 = formatZkbcTimes().parse(date1);
		Date dt2 = formatZkbcTimes().parse(date2);
		days = (dt1.getTime() - dt2.getTime());
		if (days == 0) {
			result = 0;
		} else if (days < 0) {
			result = -1;
		} else if (days > 0) {
			result = 1;
		}
		return result;
	}

	/**
	 * 日期时间比较
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException
	 */
	public static int comparingTwoDateTimes(String date1, String date2)
			throws ParseException {
		String s1 = date1.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "");
		String s2 = date2.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "");
		long days = 0;
		int result = 0;
		Date dt1 = formatAll().parse(s1);
		Date dt2 = formatAll().parse(s2);
		days = (dt1.getTime() - dt2.getTime());
		if (days == 0) {
			result = 0;
		} else if (days < 0) {
			result = -1;
		} else if (days > 0) {
			result = 1;
		}
		return result;
	}

	/**
	 * @param date
	 * @return
	 */
	public static String getTimeString(Date date) {

		return formatZkbcTimes().format(date);
	}

	/**
	 * 获取月部分
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonth(String date) {
		Date d = DateUtil.getDateFromDbString(date);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		return c.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取月部分
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取年部分
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(String date) {
		Date d = DateUtil.getDateFromDbString(date);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		return c.get(Calendar.YEAR);
	}

	/**
	 * 获取年部分
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.YEAR);
	}

	/**
	 * 获取日部分
	 * 
	 * @param date
	 * @return
	 */
	public static int getDay(String date) {
		Date d = DateUtil.getDateFromDbString(date);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		return c.get(Calendar.DAY_OF_MONTH);

	}

	/**
	 * 获取日部分
	 * 
	 * @param date
	 * @return
	 */
	public static int getEndDayInMonth(String date) {
		Date d = DateUtil.getDateFromDbString(date);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		return c.getActualMaximum(Calendar.DAY_OF_MONTH);

	}

	/**
	 * 返回当前时间
	 * 
	 * @return
	 */
	public static String getNowTimeString() {
		return getTimeString(new Date());
	}

	public static int getQuaterMonth(Date date) {

		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int thisMonth = c.get(Calendar.MONTH);
		if (thisMonth < 3) {
			return 3;
		}
		if ((thisMonth > 3) && (thisMonth < 6)) {
			return 6;
		}
		if ((thisMonth > 6) && (thisMonth < 9)) {
			return 9;
		}
		if ((thisMonth > 9) && (thisMonth < 12)) {
			return 12;
		}
		return thisMonth;
	}

	public static int getHalfYearMonth(Date date) {

		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int thisMonth = c.get(Calendar.MONTH);
		if (thisMonth < 6) {
			return 6;
		}
		if ((thisMonth > 6) && (thisMonth < 12)) {
			return 12;
		}
		return thisMonth;
	}

	/**
	 * 返回输入日期月的最后一天
	 * 
	 * @param inputDate
	 * @return
	 */
	public static Date getLastDayInMonth(Date inputDate) {
		Calendar c = Calendar.getInstance();
		c.setTime(inputDate);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		return c.getTime();
	}

	/**
	 * 返回输入日期月份有多少天。 例如2月可能有28
	 * 
	 * @param inputDate
	 * @return
	 */
	public static int getDayCountInOneMounth(Date inputDate) {
		Calendar c = Calendar.getInstance();
		c.setTime(inputDate);
		return c.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 返回输入日期月份有多少天。 例如2月可能有28
	 * 
	 * @param inputDate
	 * @return
	 */
	public static int getDayCountInOneMounth(String inputDate) {
		return getDayCountInOneMounth(DateUtil.getDateFromDbString(inputDate));
	}

	/**
	 * 指定一月的某一天,返回当天日。
	 * 
	 * @param input
	 * @param dayCount
	 * @return 日期型
	 */
	public static Date getDateByDayCount(Date input, int dayCount) {
		Calendar c = Calendar.getInstance();
		c.setTime(input);
		int maxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		dayCount = dayCount > maxDay ? maxDay : dayCount;
		c.set(Calendar.DAY_OF_MONTH, dayCount);
		return c.getTime();
	}

	/**
	 * 取下个月的某天日期
	 * 
	 * @param lastMonthDay
	 * @param dayCount
	 * @return
	 */
	public static String getNextMonthDate(String lastMonthDay, int dayCount) {
		String nextMonthDate = getNextMonthEnd(lastMonthDay);
		nextMonthDate = getDateStringToDb(getDateByDayCount(nextMonthDate,
				dayCount));
		return nextMonthDate;
	}

	/**
	 * 指定一月的某一天,返回当天日。
	 * 
	 * @param input
	 * @param dayCount
	 * @return 日期型
	 */
	public static Date getDateByDayCount(String input, int dayCount) {
		return getDateByDayCount(DateUtil.getDateFromDbString(input), dayCount);
	}

	/**
	 * 
	 * @param date
	 * @return 判断是否为月末
	 */
	public static boolean isEndMonth(String date) {
		Date toDate = DateUtil.getDateFromDbString(date);
		Calendar c = Calendar.getInstance();
		c.setTime(toDate);
		int monthEnd = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		String monthEndStr = date.substring(8);
		if (monthEndStr.charAt(0) == '0') {
			monthEndStr = String.valueOf(monthEndStr.charAt(1));
		}
		int trueMonthEnd = Integer.parseInt(monthEndStr);
		return trueMonthEnd == monthEnd;
	}

	/**
	 * 
	 * @param date
	 * @return 判断是否为年末 12-31号
	 */
	public static boolean isEndYear(String date) {
		StringTokenizer str = new StringTokenizer(date, "-");
		String year = str.nextToken();
		String month = str.nextToken();
		String day = str.nextToken();

		Calendar dealDate = Calendar.getInstance();

		dealDate.set(Calendar.YEAR, Integer.parseInt(year));
		dealDate.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		dealDate.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));

		int monthNow = dealDate.get(Calendar.MONTH);
		dealDate.set(Calendar.DAY_OF_YEAR,
				dealDate.get(Calendar.DAY_OF_YEAR) + 1);

		int monthNext = dealDate.get(Calendar.MONTH);
		if (monthNow == 11 && monthNext == 0) {
			if (monthNext == 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 * @param date
	 * @return 判断是否为季末
	 */
	public static boolean isEndQuarter(String date) {
		StringTokenizer str = new StringTokenizer(date, "-");
		String year = str.nextToken();
		String month = str.nextToken();
		String day = str.nextToken();

		Calendar dealDate = Calendar.getInstance();

		dealDate.set(Calendar.YEAR, Integer.parseInt(year));
		dealDate.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		dealDate.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));

		int monthEnd = dealDate.getActualMaximum(Calendar.DAY_OF_MONTH);
		int iMonth = Integer.parseInt(month);
		if (iMonth % 3 == 0 && monthEnd == Integer.parseInt(day)) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否周末
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isEndWeek(String date) {

		Date bdate = getDateFromDbString(date);
		Calendar cal = Calendar.getInstance();
		cal.setTime(bdate);
		if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
				|| cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否是周一
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isMonday(String date) {

		Date bdate = getDateFromDbString(date);
		Calendar cal = Calendar.getInstance();
		cal.setTime(bdate);
		if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * @param date
	 * @return 将日期格式转换为String类型,并填充。例如:2009-5-5 ---> 2009-05-05
	 */
	public static String getDateStringToDb(Date date) {
		return formatZkbc().format(date);
	}

	public static String getDateTimeStringToDb(Date date) {
		return formatDayTimeZkbc().format(date);
	}

	/**
	 * 
	 * @param date
	 * @return 将日期格式转换为String类型,并填充。例如:2009-5-5 ---> 2009-05-05
	 */
	public static String getTimeStringToDb(Date date) {
		return formatZkbcTimes().format(date);
	}

	/**
	 * 
	 * @param date
	 * @return 将日期格式转换为String类型,并填充。例如:2009-5-5 ---> 2009-05-05
	 */
	public static String getDateTimeString(Date date) {
		return formatAll().format(date);
	}

	/**
	 * 把数据库的字符串转化成Date型
	 * 
	 * @param dateString
	 *            字符串格式(yyyy-MM-dd)
	 * @return
	 */
	public static Date getDateFromDbString(String dateString) {
		try {
			return formatZkbc().parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException("string format error!! ,错误日期数据:"
					+ dateString);
		}
	}

	/**
	 * 把数据库的字符串转化成Date型
	 * 
	 * @param dateString
	 * 
	 * @return
	 */
	public static Date getDateTimeFromDbString(String dateString) {
		try {
			return formatDayTimeZkbc().parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException("string format error!! ,错误日期数据:"
					+ dateString);
		}
	}

	/**
	 * 返回当期日期的字符串
	 * 
	 * @return
	 */
	public static String getNowDateString() {
		return getDateStringToDb(new Date());
	}

	/**
	 * 返回当期日期时间的字符串
	 * 
	 * @return
	 */
	public static String getNowDateTimeString() {
		return getDateTimeString(new Date());
	}

	public static final Locale DEFAUTL_LOCALE = Locale.ENGLISH;

	public static SimpleDateFormat formatZkbcTimes() {
		return new SimpleDateFormat("HH:mm:ss", DEFAUTL_LOCALE);
	}

	public static SimpleDateFormat fomatStd() {
		return new SimpleDateFormat("yyyy/MM/dd", DEFAUTL_LOCALE);
	}

	public static SimpleDateFormat formatZkbc() {
		return new SimpleDateFormat("yyyy-MM-dd", DEFAUTL_LOCALE);
	}

	public static SimpleDateFormat formatDayTimeZkbc() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", DEFAUTL_LOCALE);
	}

	public static SimpleDateFormat formatAll() {
		return new SimpleDateFormat("yyyyMMddHHmmss", DEFAUTL_LOCALE);
	}

	/**
	 * 是否同月年
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isYearAndMonthSame(String date1, String date2) {
		if (getMonth(date1) != getMonth(date2)) {
			return false;
		}
		if (getYear(date1) != getYear(date2)) {
			return false;
		}
		return true;
	}

	/**
	 * 判断date1和date2是否年和月相同,如果相同就返回date2,否则返回date1
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static java.sql.Date isEqualsYM(java.sql.Date date1,
			java.sql.Date date2) {
		Calendar date1Temp = Calendar.getInstance();
		Calendar date2Temp = Calendar.getInstance();
		date1Temp.setTime(date1);
		date2Temp.setTime(date2);
		if ((date1Temp.get(Calendar.YEAR) == date2Temp.get(Calendar.YEAR))
				&& date1Temp.get(Calendar.MONTH) == date2Temp
						.get(Calendar.MONTH)) {
			date1 = date2;
		}
		return date1;
	}

	/**
	 * 阶段描述
	 * 
	 * @author hb
	 * 
	 */
	public static class TermDes {
		public int termCount = 0;
		public int accualDayCount = 0;
		public java.util.Date billDay;
		public java.sql.Date someDebitDay;
		public boolean termPlusDay = false;
	}

	/**
	 * 如果起息日是31,但是到了2月却只有28天,加频率加出来就有问题。
	 * 
	 * @param result
	 * @param dueDay
	 * @return
	 */
	public static Calendar adjustByDueDay(Calendar result, int dueDay) {
		if (dueDay > 28) {
			int curDueDay = result.get(Calendar.DAY_OF_MONTH);
			if (curDueDay != dueDay) {
				int max = result.getActualMaximum(Calendar.DAY_OF_MONTH);
				curDueDay = dueDay;
				if (max < dueDay) {
					curDueDay = max;
				}
				result.set(Calendar.DAY_OF_MONTH, curDueDay);
			}
		}
		return result;
	}

	/**
	 * 如果起息日是31,但是到了2月却只有28天,加频率加出来就有问题。
	 * 
	 * @param result
	 * @param dueDay
	 * @return
	 */
	public static Date adjustByDueDay(Date result, int dueDay) {
		Calendar newResult = Calendar.getInstance();
		newResult.setTime(result);
		return adjustByDueDay(newResult, dueDay).getTime();
	}

	/**
	 * 是否月初
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isFirstMonth(String date) {
		return Integer.parseInt(date.split("-")[2]) == 1;
	}

	/**
	 * 获取月末日期
	 * 
	 * @param date
	 * @return
	 */
	public static String getEndMonth(String date) {
		StringTokenizer str = new StringTokenizer(date, "-");

		int year = Integer.parseInt(str.nextToken());
		int month = Integer.parseInt(str.nextToken());

		Calendar cal = Calendar.getInstance();
		// 年
		cal.set(Calendar.YEAR, year);
		// 月,因为Calendar里的月是从0开始,所以要减1
		cal.set(Calendar.MONTH, month - 1);
		// 日,设为一号
		cal.set(Calendar.DATE, 1);
		// 月份加一,得到下个月的一号
		cal.add(Calendar.MONTH, 1);
		// 下一个月减一为本月最后一天
		cal.add(Calendar.DATE, -1);

		return formatZkbc().format(cal.getTime());
	}

	/**
	 * 获取月初日期
	 * 
	 * @param date
	 * @return
	 */
	public static String getBeginMonth(String date) {

		Calendar cal = getCalendarObject(date);
		// 日,设为一号
		cal.set(Calendar.DATE, 1);

		return formatZkbc().format(cal.getTime());
	}

	/**
	 * 获取每月21日
	 *
	 * @param date
	 * @return
	 */
	public static String getDay21(String date) {

		Calendar cal = getCalendarObject(date);
		// 日,设为一号
		cal.set(Calendar.DATE, 21);

		return formatZkbc().format(cal.getTime());
	}

	/**
	 * 获取上月21日
	 *
	 * @param date
	 * @return
	 */
	public static String getLastDay21(String date) {

		Calendar cal = getCalendarObject(date);
		// 月份减一,得到上个月的一号
		cal.add(Calendar.MONTH, -1);
		// 日,设为一号
		cal.set(Calendar.DATE, 21);

		return formatZkbc().format(cal.getTime());

	}

	/**
	 * 获取当前日期的Calendar对象
	 *
	 * @param date
	 * @return
	 */
	public static Calendar getCalendarObject(String date) {

		StringTokenizer str = new StringTokenizer(date, "-");

		int year = Integer.parseInt(str.nextToken());
		int month = Integer.parseInt(str.nextToken());
		int day = Integer.parseInt(str.nextToken());

		Calendar cal = Calendar.getInstance();
		// 年
		cal.set(Calendar.YEAR, year);
		// 月,因为Calendar里的月是从0开始,所以要减1
		cal.set(Calendar.MONTH, month - 1);
		// 日,设为一号
		cal.set(Calendar.DATE, day);

		return cal;
	}

	/**
	 * 获取上月月初日期
	 *
	 * @param date
	 * @return
	 */
	public static String getLastBeginMonth(String date) {

		Calendar cal = getCalendarObject(date);
		// 月份减一,得到上个月的一号
		cal.add(Calendar.MONTH, -1);
		// 日,设为一号
		cal.set(Calendar.DATE, 1);

		return formatZkbc().format(cal.getTime());
	}

	/**
	 * 获取下月月初日期
	 *
	 * @param date
	 * @return
	 */
	public static String getNextMonthEnd(String date) {

		Calendar cal = getCalendarObject(date);
		// 月份加一,得到下个月的一号
		cal.add(Calendar.MONTH, 1);
		// 日,设为一号
		cal.set(Calendar.DATE, 1);

		return formatZkbc().format(cal.getTime());
	}

	/**
	 * 获取下月月末日期
	 * 
	 * @param date
	 * @return
	 */
	public static String getNextMonthLast(String date) {
		date = getNextMonthEnd(date);
		date = getEndMonth(date);

		return date;
	}

	/**
	 * 获取两个日期之间的月数
	 * 
	 * @param frmDate
	 * @param endDate
	 * @return
	 */
	public static int getMonthCountBetweenDate(String frmDate, String endDate) {
		Calendar cStart = Calendar.getInstance();
		Calendar cEnd = Calendar.getInstance();
		cStart.setTime(DateUtil.getDateFromDbString(frmDate));
		cEnd.setTime(DateUtil.getDateFromDbString(endDate));
		int sYear = cStart.get(Calendar.YEAR);
		int eYear = cEnd.get(Calendar.YEAR);
		int sMonth = cStart.get(Calendar.MONTH);
		int eMonth = cEnd.get(Calendar.MONTH);
		int sDate = cStart.get(Calendar.DATE);
		int eDate = cEnd.get(Calendar.DATE);
		int addMonth = 0;
		if (eDate > sDate) {
			addMonth = 1;
		}
		return (eYear - sYear) * 12 + (eMonth - sMonth) + addMonth;
	}

	/**
	 * 获取两个日期之间的季数
	 * 
	 * @param frmDate
	 * @param endDate
	 * @return
	 */
	public static int getQuaterCountBetweenDate(String frmDate, String endDate) {
		int months = getMonthCountBetweenDate(frmDate, endDate);
		int i = months / 3;
		int j = months % 3;
		if (j != 0) {
			i++;
		}
		return i;
	}

	/**
	 * 当前业务日期+/-count年
	 * 
	 * @param date
	 * @param count
	 * @return
	 */
	public static String getDateOfCountYear(String date, int count) {

		Calendar cal = getCalendarObject(date);

		cal.add(Calendar.YEAR, count);

		return formatZkbc().format(cal.getTime());
	}

	/**
	 * 当前业务日期+/-count月
	 * 
	 * @param date
	 * @param count
	 * @return
	 */
	public static String getDateOfCountMonth(String date, int count) {

		Calendar cal = getCalendarObject(date);

		cal.add(Calendar.MONTH, count);

		return formatZkbc().format(cal.getTime());
	}

	/**
	 * 返回。输入日期+dayCount 输入负数,就是减少天数
	 * 
	 * @param inputDate
	 * @return
	 */
	public static String getDateIncDayCount(String inputDate, int dayCount) {
		Date d = DateUtil.getDateFromDbString(inputDate);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.DAY_OF_YEAR, dayCount);
		return DateUtil.getDateStringToDb(c.getTime());
	}

	/**
	 * 返回。输入日期+dayCount 输入负数,就是减少天数
	 * 
	 * @param inputDate
	 * @return
	 */
	public static String getTimeIncDayCount(String inputDate, int dayCount) {
		Date d = DateUtil.getDateTimeFromDbString(inputDate);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.HOUR_OF_DAY, dayCount);
		return DateUtil.getDateTimeStringToDb(c.getTime());
	}

	public static String getMinIncDayCount(String inputDate, int dayCount) {
		Date d = DateUtil.getDateTimeFromDbString(inputDate);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.MINUTE, dayCount);
		return DateUtil.getDateTimeStringToDb(c.getTime());
	}

	/**
	 * 取下一年的1月1日
	 * 
	 * @param date
	 * @return
	 */
	public static String getNextYearBegin(String date) {

		Calendar cal = getCalendarObject(date);
		cal.add(Calendar.YEAR, 1);
		cal.set(Calendar.MONTH, 0);
		// 日,设为一号
		cal.set(Calendar.DATE, 1);

		return formatZkbc().format(cal.getTime());
	}

	/**
	 * 按季调整利率,取每季初的:4月1 ,7月1, 10月1, 1月1
	 * 
	 * @param date
	 * @return
	 */
	public static String getRepcDate(String date) {

		StringTokenizer str = new StringTokenizer(date, "-");

		int year = Integer.parseInt(str.nextToken());
		int month = Integer.parseInt(str.nextToken());
		int newMonth = 0;
		if (month >= 1 && month <= 3){
			newMonth = 4;
		}
		if (month >= 4 && month <= 6){
			newMonth = 7;
		}
		if (month >= 7 && month <= 9){
			newMonth = 10;
		}
		if (month >= 10 && month <= 12) {
			year = year + 1;
			newMonth = 1;
		}

		Calendar cal = Calendar.getInstance();
		// 年
		cal.set(Calendar.YEAR, year);
		// 月,因为Calendar里的月是从0开始,所以要减1
		cal.set(Calendar.MONTH, newMonth - 1);
		// 日,设为一号
		cal.set(Calendar.DATE, 1);

		return formatZkbc().format(cal.getTime());
	}

	/**
	 * 按季计息,取下一个季度的21日,如3月21,6月21,9月21,12月21,如果参数进来恰好是计息日,则往后取最近下一个季度的21日,如3月21日
	 * , 就返回6月21日 add by liaozy 2014.2.12
	 * 
	 * @param date
	 * @return
	 */
	public static String getQuaterDay21(String date) {

		StringTokenizer str = new StringTokenizer(date, "-");

		int year = Integer.parseInt(str.nextToken());
		int month = Integer.parseInt(str.nextToken());
		int day = Integer.parseInt(str.nextToken());
		int newMonth = 0;
		if (month == 12 && day >= 21) {
			year = year + 1;
		}
		// 1-3月 12月21-3.20取3.21
		if (month >= 1 && month <= 2) {
			newMonth = 3;
		}
		if (month == 3 && day < 21) {
			newMonth = 3;
		}
		if (month == 12 && day >= 21) {
			newMonth = 3;
		}
		if (month == 3 && day >= 21) {
			newMonth = 6;
		}
		// 4-6月 4.1-4.20取4.21
		if (month >= 4 && month <= 5) {
			newMonth = 6;
		}
		if (month == 6 && day < 21) {
			newMonth = 6;
		}
		if (month == 6 && day >= 21) {
			newMonth = 9;
		}
		// 7-9月 7.1-9.20取9.21
		if (month >= 7 && month <= 8) {
			newMonth = 9;
		}
		if (month == 9 && day < 21) {
			newMonth = 9;
		}
		if (month == 9 && day >= 21) {
			newMonth = 12;
		}
		// 10-12月 10.1-12.20返回12.21
		if (month >= 10 && month <= 11) {
			newMonth = 12;
		}
		if (month == 12 && day < 21) {
			newMonth = 12;
		}

		Calendar cal = Calendar.getInstance();
		// 年
		cal.set(Calendar.YEAR, year);
		// 月,因为Calendar里的月是从0开始,所以要减1
		cal.set(Calendar.MONTH, newMonth - 1);
		// 日,设为21号
		cal.set(Calendar.DATE, 21);

		return formatZkbc().format(cal.getTime());
	}

	/**
	 * 8位转10位 格式为2013-11-25
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDateH(String date) {
		return DateUtil.getDateStringToDb(DateUtil.getDateFromDbString(date));
	}



	/**
	 * 将日期转换为中文
	 * 
	 * @param date
	 * @return
	 */
	public static String toChineseDate(String date) {
		if (date == null || "".equals(date)){
			return "";
		}
		String[] str = date.split("-");
		if (date.replace("-", "").length() != 8){
			return "";
		}
			
		String sReturn = "";
		sReturn = str[0] + "年";
		sReturn += str[1] + "月";
		sReturn += str[2] + "日";
		return sReturn;
	}

	public static SimpleDateFormat coreFnt() {
		return new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
	}

	public static String getFileDateString(Date date) {
		return coreFnt().format(date);
	}

	public static String getFileDateString(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = null;
		try {
			date1 = sdf.parse(date).toString();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date1;
	}

	public static String getFileTimeString(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String times = null;
		try {
			times = sdf.parse(time).toString();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return times;
	}
	
	public static String getYYYYMMDDDate(Date date) {
		if(null==date){
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String ymd=sdf.format(date);
		return ymd;
	}

	public static String getHHMMSSDate(Date time) {
		if(null==time){
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String hms=sdf.format(time);
		return hms;
	}


	/**
	 * 判断是否为当月的15日
	 * @return
	 */
	public static boolean getCurrentDayIsFift(){
		Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DATE);
        if(day==15){
        	return true;
        }else{
        	return false;
        }
	}
	/**
	 * 
	 * @param now
	 * @param day
	 * @return
	 */
	public static String addDay(Date now, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		// 把日期往后增加一天.整数往后推,负数往前移动
		calendar.add(Calendar.DATE, day);
		return getDateStringToDb(calendar.getTime());
	}
	
	public static void main(String[] args) {
		try {
			System.out.println(toChineseDate("2018-06-07"));

			System.out.println(calDate("2018/06/07", -1, "dd"));

		} catch (ParseException e) {

			e.printStackTrace();
		}
	}
	public static String getTimeIncDayCount2(String inputDate, int dayCount) {
		 SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		 Date date;
		 String dat="";
			try {
				date = sf.parse(inputDate);
				Calendar calendar = new GregorianCalendar(); 
			      calendar.setTime(date);
				//把日期往后增加一天.整数往后推,负数往前移动
			      calendar.add(Calendar.DATE,dayCount);
				//这个时间就是日期往后推一天的结果
			      date=calendar.getTime();
			      dat=sf.format(date);
			} catch (ParseException e) {

				e.printStackTrace();
			}
	      return dat;
	}
}

  

转载于:https://www.cnblogs.com/HHR-SUN/p/11361823.html