JAVA日期操作类封装(用于个人记录,日后有需要再调整,若有不足,请指出,非常感谢)
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* <p>Title: DateUtil.java</p>
* <p>Description: 时间工具类</p>
* @author coder player
* @date 2019-10-23
*/
public class DateUtil {
/**
* 日期字符的年数运算
* @param beginDate
* @param format
* @param diff
* @return
* @throws Exception
*/
public static String addYearDateStr(String beginDate, String format, int diff) throws Exception{
return calDateStr(beginDate, format, Calendar.YEAR, diff);
}
/**
* 日期字符的月数运算
* @param beginDate
* @param format
* @param diff
* @return
* @throws Exception
*/
public static String addMonthDateStr(String beginDate, String format, int diff) throws Exception{
return calDateStr(beginDate, format, Calendar.MONTH, diff);
}
/**
* 日期字符的天数运算
* @param beginDate
* @param format
* @param diff
* @return
* @throws Exception
*/
public static String addDayDateStr(String beginDate, String format, int diff) throws Exception{
return calDateStr(beginDate, format, Calendar.DAY_OF_MONTH, diff);
}
/**
* 计算日期字符
* @param beginDate 日期字符串
* @param format 日期格式
* @param field 增加类型:年月日
* @param diff 增加数量
* @return
* @throws Exception
*/
public static String calDateStr(String beginDate, String format, int field, int diff) throws Exception{
Date date = strFormatDate(beginDate, format);
date = add(date, field, diff);
return dateFormatStr(date, format);
}
/**
* 日期格式转换:
* 例如:从yyyy-MM-dd变为yyyy-mm
* @param date
* @param beginFormat
* @param endFormate
* @return
* @throws Exception
*/
public static String changeDateFormat(String dateStr, String beginFormat, String endFormate) throws Exception{
return dateFormatStr(strFormatDate(dateStr, beginFormat), endFormate);
}
/**
* 日期格式转换,转后依然是日期类型
* @param dateStr
* @param beginFormat
* @param endFormate
* @return
* @throws Exception
*/
public static Date changeDate2Date(String dateStr, String beginFormat, String endFormate) throws Exception{
return strFormatDate(dateFormatStr(strFormatDate(dateStr, beginFormat), endFormate), endFormate);
}
/**
* 日期转字符
* @param date
* @param formatStr
* @return
* @throws Exception
*/
public static String dateFormatStr(Date date, String formatStr) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
return sdf.format(date);
}
/**
* 字符转日期
* @param dateStr
* @param formatStr
* @return
* @throws Exception
*/
public static Date strFormatDate(String dateStr, String formatStr) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
return sdf.parse(dateStr);
}
/**
* 增加年份
* @param date
* @param diff
* @return
*/
public static Date addYear(Date date, int diff){
return add(date,Calendar.YEAR,diff);
}
/**
* 增加月份
* @param date
* @param diff
* @return
*/
public static Date addMonth(Date date, int diff){
return add(date,Calendar.MONTH,diff);
}
/**
* 增加日
* @param date
* @param diff
* @return
*/
public static Date addDay(Date date, int diff){
return add(date,Calendar.DAY_OF_MONTH,diff);
}
/**
* 年月日增加,通用接口
* @param date
* @param field
* @param diff
* @return
*/
public static Date add(Date date, int field, int diff){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(field, diff);
date = c.getTime();
return date;
}
/**
* 获取年
* @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 getMonth(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.MONTH)+1;
}
/**
* 获取日
* @param date
* @return
*/
public static int getDay(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.DAY_OF_MONTH);
}
}