package foundation.util;
|
|
import java.text.DateFormat;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
|
public class DateTimeUtil {
|
|
private static String DATEFORMAT_YYYYMMDD = "yyyy-MM-dd";
|
private static String TIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
|
|
// 获取指定秒数后的日期
|
public static String addSecond(String dateStr, int seconds) throws Exception {
|
SimpleDateFormat sdf = new SimpleDateFormat(TIMEFORMAT);
|
Calendar calendar = Calendar.getInstance();
|
|
Date date = sdf.parse(dateStr);
|
|
calendar.setTime(date);
|
|
calendar.add(Calendar.SECOND, seconds);
|
|
return sdf.format(calendar.getTime());
|
}
|
|
// 获取指定天数后的日期
|
public static String addDays(String dateStr, int days) throws Exception {
|
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_YYYYMMDD);
|
Calendar calendar = Calendar.getInstance();
|
|
Date date = sdf.parse(dateStr);
|
|
calendar.setTime(date);
|
|
calendar.add(Calendar.DAY_OF_MONTH, days);
|
|
return sdf.format(calendar.getTime());
|
}
|
|
// 获取指定月份数后的日期
|
public static String addMonths(String dateStr, int months) throws Exception {
|
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_YYYYMMDD);
|
Calendar calendar = Calendar.getInstance();
|
|
Date date = sdf.parse(dateStr);
|
|
calendar.setTime(date);
|
|
calendar.add(Calendar.MONTH, months);
|
|
return sdf.format(calendar.getTime());
|
}
|
|
|
// 获取指定年份数后的日期
|
public static String addYears(String dateStr, int years) throws Exception {
|
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_YYYYMMDD);
|
Calendar calendar = Calendar.getInstance();
|
|
Date date = sdf.parse(dateStr);
|
|
calendar.setTime(date);
|
|
calendar.add(Calendar.YEAR, years);
|
|
return sdf.format(calendar.getTime());
|
}
|
|
public static int getYearInt(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
int year = calendar.get(1);
|
return (year);
|
}
|
|
// 获取指定日期的季节
|
public static String getSeason(String dateStr) throws Exception {
|
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_YYYYMMDD);
|
Calendar calendar = Calendar.getInstance();
|
|
Date date = sdf.parse(dateStr);
|
|
calendar.setTime(date);
|
|
int season = calendar.get(Calendar.MONTH)/3 + 1;
|
return String.valueOf(season);
|
}
|
|
// 获取指定日期的季节
|
public static String getSeason(Date date) throws Exception {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
|
int season = calendar.get(Calendar.MONTH)/3 + 1;
|
return String.valueOf(season);
|
}
|
|
public static Integer getQuarter(Date date) {
|
String monthStr = getMonth(date, false);
|
Integer month = Integer.valueOf(monthStr);
|
if (month == null) {
|
return null;
|
}
|
return (((month -1) / 3) + 1);
|
}
|
|
public static int getMonth(Date date) throws Exception {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
int month = calendar.get(2) + 1;
|
return (month);
|
}
|
|
// 获取指定日期的月份
|
public static String getMonth(String dateStr, boolean withZero) throws Exception {
|
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_YYYYMMDD);
|
Calendar calendar = Calendar.getInstance();
|
|
Date date = sdf.parse(dateStr);
|
|
calendar.setTime(date);
|
|
int month = calendar.get(Calendar.MONTH) + 1;
|
|
if(!withZero) {
|
return String.valueOf(month);
|
}
|
|
if(month < 10) {
|
return "0" + month;
|
}
|
return String.valueOf(month);
|
}
|
|
public static String getMonth(Date date, boolean withZero) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
|
int month = calendar.get(Calendar.MONTH) + 1;
|
|
if(!withZero) {
|
return String.valueOf(month);
|
}
|
|
if(month < 10) {
|
return "0" + month;
|
}
|
return String.valueOf(month);
|
}
|
|
// 获取指定日期的年份
|
public static String getYear(String dateStr) throws Exception {
|
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_YYYYMMDD);
|
Calendar calendar = Calendar.getInstance();
|
|
Date date = sdf.parse(dateStr);
|
|
calendar.setTime(date);
|
|
int year = calendar.get(Calendar.YEAR);
|
return String.valueOf(year);
|
}
|
|
// 获取指定日期的年份
|
public static String getYear(Date date) throws Exception {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
|
int year = calendar.get(Calendar.YEAR);
|
return String.valueOf(year);
|
}
|
|
// 获取两个日期之间相隔月份
|
@SuppressWarnings("deprecation")
|
public static int getIntervalMonth(Date startDate, Date endDate) throws Exception {
|
if (startDate == null) {
|
startDate = new Date();
|
}
|
if (endDate == null) {
|
endDate = new Date();
|
}
|
return (endDate.getYear() - startDate.getYear()) * 12 + endDate.getMonth() - startDate.getMonth();
|
}
|
|
public static int getIntervalDays(Date startDate, Date endDate) {
|
if (startDate == null) {
|
startDate = new Date();
|
}
|
if (endDate == null) {
|
endDate = new Date();
|
}
|
|
return (int) ((endDate.getTime() - startDate.getTime()) / (1000*3600*24));
|
}
|
|
public static String addMonths(Date date, int months, String returnYM) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
int month = calendar.get(Calendar.MONTH) + 1;
|
month = month + months;
|
int year = calendar.get(Calendar.YEAR);
|
|
if(month > 12) {
|
month = month/12;
|
year = year + 1;
|
}
|
|
if (month < 10) {
|
return year + "0" + month;
|
}
|
|
return year + month + "";
|
}
|
|
public static Date getMonthStartDate(String monthly) throws Exception {
|
String date = monthly + "01";
|
return Util.StringToDate(date);
|
}
|
|
public static Date getMonthEndDate(String monthly) throws Exception {
|
String date = monthly + "01";
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(Util.StringToDate(date));
|
int month = calendar.get(Calendar.MONTH);
|
|
calendar.set(Calendar.MONTH, month + 1);
|
calendar.set(Calendar.DATE, 1);
|
calendar.add(Calendar.DATE, -1);
|
|
return calendar.getTime();
|
}
|
|
public static Date getQuarterEndDate(Date now) throws Exception {
|
if (now == null) {
|
return null;
|
}
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.set(getYearInt(now), (getQuarter(now) * 3 - 1) , 1);
|
|
int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
|
calendar.set(Calendar.DAY_OF_MONTH, actualMaximum);
|
return calendar.getTime();
|
}
|
|
public static Date getQuarterStartDate(Date now) {
|
if (now == null) {
|
return null;
|
}
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.set(getYearInt(now), ((getQuarter(now) - 1 )* 3) , 1);
|
|
return calendar.getTime();
|
}
|
|
public static Date StringToDate(String date, String format) throws ParseException {
|
if (date == null) {
|
return null;
|
}
|
|
Date result = null;
|
DateFormat dateFormat = new SimpleDateFormat(format);
|
|
result = dateFormat.parse(date);
|
|
return result;
|
}
|
}
|