import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* 日期工具类
*/
public class DateUtil {
public final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 获取日期格式化对象
* @param partten
* @return
*/
public static SimpleDateFormat getFormat(String partten){
SimpleDateFormat dateFormat = new SimpleDateFormat(partten);
return dateFormat;
}
/**
* 获取(yyyy-MM-dd HH:mm:ss格式)当前日期时间
* @return
*/
public static String getCurDateTime() {
return sdf.format(new Date());
}
/**
* 获取指定格式的当前日期字符串
*
* @param format
* @return
*/
public static String getFormatDate(String format){
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.format(new Date());
}
/**
* 将日期转为指定格式的字符串
*
* @param format "yyyy-MM-dd HH:mm:ss"
* @return
*/
public static String getStrDate(String format,Date date){
try {
return getFormat(format).format(date);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将日期字符串转为指定格式日期
*
* @param time
* @param partten
* @return
*/
public static Date stringToDate(String time, String partten) {
try {
return getFormat(partten).parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 验证日期字符串能否转为特定格式
*
* @param format 要转换成的日期格式
* @param strDate 要转换的字符串
* @return
*/
public static boolean isValidDate(String format, String strDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
dateFormat.setLenient(false);//指定日期/时间解释是否是宽松的
try {
dateFormat.parse(strDate);
return true;
} catch (ParseException e) {
return false;
}
}
/**
* 日期运算,返回指定格式日期字符串
*
* @param date 日期字符串
* @param format 输出格式
* @param type 操作类型
* @param num 操作值 月(M)和分钟(m)区分大小写
* @return
*/
public static String dateAdd(String date, String format, String type, int num) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
GregorianCalendar gc = new GregorianCalendar();
try {
gc.setTime(sdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
if ("D".equals(type) || "d".equals(type)) {//日
gc.add(Calendar.DATE, num);
} else if ("M".equals(type)) {//月
gc.add(Calendar.MONTH, num);
} else if ("Y".equals(type) || "y".equals(type)) {//年
gc.add(Calendar.YEAR, num);
} else if ("H".equals(type) || "h".equals(type)) {//时
gc.add(Calendar.HOUR, num);
} else if ("m".equals(type)) {//分
gc.add(Calendar.MINUTE, num);
} else if ("S".equals(type) || "s".equals(type)) {
gc.add(Calendar.SECOND, num);
}
return new SimpleDateFormat(format).format(gc.getTime());
}
/**
* 日期运算
*
* @param date
* @param type 月(M)和分钟(m)区分大小写
* @param num
* @return
*/
public static Date dateAdd(Date date, String type, int num) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
if ("D".equals(type) || "d".equals(type)) {//日
gc.add(Calendar.DATE, num);
} else if ("M".equals(type)) {//月
gc.add(Calendar.MONTH, num);
} else if ("Y".equals(type) || "y".equals(type)) {//年
gc.add(Calendar.YEAR, num);
} else if ("H".equals(type) || "h".equals(type)) {//时
gc.add(Calendar.HOUR, num);
} else if ("m".equals(type)) {//分
gc.add(Calendar.MINUTE, num);
} else if ("S".equals(type) || "s".equals(type)) {
gc.add(Calendar.SECOND, num);
}
return gc.getTime();
}
/**
* 查询日期为周几
*
* @param dt 要查询的日期
* @return
* @throws Exception
*/
public static int getDayOfWeek(Date dt) throws Exception {
Calendar c = Calendar.getInstance();
c.setTime(dt);
int dayForWeek = 0;
if(c.get(Calendar.DAY_OF_WEEK) == 1){
dayForWeek = 7;
}else{
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
return dayForWeek;
}
/**
* TODO 查询日期为星期几
*
* @param dt 要查询的日期
* @return
*/
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
*
* 计算两个日期相差多少天
*
* @param dateBefore
* @param dateAfter
* @return
*/
public static int dateSubtraction(Date dateBefore, Date dateAfter) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(dateBefore);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(dateAfter);
int day1 = cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
int year1 = cal1.get(Calendar.YEAR);
int year2 = cal2.get(Calendar.YEAR);
if (year1 != year2) // 同一年
{
int timeDistance = 0;
for (int i = year1; i < year2; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
{
timeDistance += 366;// 闰年
} else
{
timeDistance += 365;// 不是闰年
}
}
return timeDistance + (day2 - day1);
} else // 不同年
{
return day2 - day1;
}
}
}