/**
* 将日期,向前或向后计算n天
* @param date 传入的日期
* @param oldFormat 传入的日期格式
* @param isAfter 向后计算ture,向前计算false
* @param day 计算的天数
* @param newFormat 返回的日期字符串格式
* @return 字符串日期
* @throws Exception
*/
public static String getDateBeforeOrAfterDay(String date, String oldFormat, boolean isAfter, int day, String newFormat) throws Exception{
SimpleDateFormat osdf = new SimpleDateFormat(oldFormat);
SimpleDateFormat nsdf = new SimpleDateFormat(newFormat);
Date oldDate = osdf.parse(date);
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(oldDate);
if(isAfter)
rightNow.add(Calendar.DATE,day);//要加的日期
else
rightNow.add(Calendar.DATE,-day);//要减的日期
return nsdf.format(rightNow.getTime());
}
/**
* 某月最后一天
* @param year
* @param month
* @param day
* @param formate
* @return
*/
public static String getLastDay(int year,int month,int day,String formate){
SimpleDateFormat df = new SimpleDateFormat(formate);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
calendar.add(Calendar.DAY_OF_MONTH, -1);
return df.format(calendar.getTime());
}
/**
* 得到两日期相差几个月
* @param startDate 2013-07-01
* @param endDate 2013-09-01
* @return 3
*/
public static int getMonthDiff(String startDate, String endDate) throws Exception{
int monthday;
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date startDate1 = fmt.parse(startDate);
Calendar starCal = Calendar.getInstance();
starCal.setTime(startDate1);
int sYear = starCal.get(Calendar.YEAR);
int sMonth = starCal.get(Calendar.MONTH);
Date endDate1 = fmt.parse(endDate);
Calendar endCal = Calendar.getInstance();
endCal.setTime(endDate1);
int eYear = endCal.get(Calendar.YEAR);
int eMonth = endCal.get(Calendar.MONTH);
monthday = ((eYear - sYear) * 12 + (eMonth - sMonth));
return monthday+addMonth;
}
/**
* 某个日期的下N个月
* @return
*/
public static String getNextMonth_N(String gscstr,int N) throws Exception {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(gscstr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, N);
Date theDate = calendar.getTime();
String s = df.format(theDate);
StringBuffer str = new StringBuffer().append(s).append(" 23:59:59");
return str.toString();
}
/**
* 当前日期的下一天
*/
public static String getNextDay(String gscstr) throws Exception{
return getNextDay_N(gscstr,1);
}
/**
* 当前日期的下N天
*/
public static String getNextDay_N(String gscstr,int N) throws Exception{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(gscstr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, N);
Date theDate = calendar.getTime();
String s = df.format(theDate);
StringBuffer str = new StringBuffer().append(s).append(" 00:00:00");
return str.toString();
}
/**
* 当前日期的上一天
*/
public static String getForwardDay(String gscstr) throws Exception{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(gscstr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date theDate = calendar.getTime();
String s = df.format(theDate);
StringBuffer str = new StringBuffer().append(s).append(" 00:00:00");
return str.toString();
}
/**
* 当前日期的下N年
*/
public static String getNextYear_N(String gscstr,int N) throws Exception{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(gscstr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, N);
Date theDate = calendar.getTime();
String s = df.format(theDate);
StringBuffer str = new StringBuffer().append(s).append(" 00:00:00");
return str.toString();
}
版权声明:本文为qq_35820886原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。