package com.guiyang.common.core.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
/**
* 日期工具類
*/
public class DateUtils {
public static final String DATE_FORMAT_STR1 = "yyyyMMdd";
public static final String DATE_FORMAT_STR2 = "yyyyMMddHHmmssS";
public static final String DATE_FORMAT_STR3 = "yyyy/MM/dd";
public static final String DATE_FORMAT_STR4 = "yyyy/MM/dd HH:mm:ss";
public static final String DATE_FORMAT_STR5 = "yyyy/MM/dd HH:mm:ss.S";
public static final String DATE_FORMAT_STR6 = "yyyy-MM-dd";
public static final String DATE_FORMAT_STR7 = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_STR8 = "yyyy-MM-dd HH:mm:ss.S";
public static final String DATE_FORMAT_STR9 = "yyyy年MM月dd日HH時mm分ss秒";
public static final String WEEK_MONDAY = "星期一";
public static final String WEEK_TUESDAY = "星期二";
public static final String WEEK_WEDNESDAY = "星期三";
public static final String WEEK_THURSDAY = "星期四";
public static final String WEEK_FRIDAY = "星期五";
public static final String WEEK_SATURDAY = "星期六";
public static final String WEEK_SUNDAY = "星期七";
private DateUtils() {
}
/**
* 獲取當前系統時間(返回日期格式)
* @return
*/
public static Date getCurrentTimeToDate() {
Calendar c = Calendar.getInstance();
return c.getTime();
}
/**
* 獲取當前系統時間(返回字串格式)
* @param formatStr 格式化字串
* @return
*/
public static String getCurrentTimeToStr(String formatStr) {
Calendar c = Calendar.getInstance();
return getDateToFormat(c.getTime(), formatStr);
}
/**
* 指定日期格式化
* @param date 日期
* @param formatStr 格式化字串
* @return
*/
public static String getDateToFormat(Date date, String formatStr) {
if (date == null) {
return null;
}
if (StringUtils.isBlank(formatStr)) {
return date.toString();
}
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
return sdf.format(date);
}
/**
* 字串轉換成日期格式
* @param dateStr 日期字串
* @return
*/
public static Date strConvertToDate(String dateStr, String parttern) {
SimpleDateFormat sdf = new SimpleDateFormat(parttern);
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 計算兩個日期之間相差的天數
* @param firsterDate 第一個時間
* @param secondDate 第二個時間
* @return 相差天數
*/
public static int getDaysBetween(Date firsterDate, Date secondDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
firsterDate = sdf.parse(sdf.format(firsterDate));
secondDate = sdf.parse(sdf.format(secondDate));
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(firsterDate);
long time1 = cal.getTimeInMillis();
cal.setTime(secondDate);
long time2 = cal.getTimeInMillis();
long betweenDays = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(betweenDays));
}
/**
* 獲取日期的星期
* @param date 日期
* @return
*/
public static String getWeek(Date date) {
String week = "";
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int weekNumber = calendar.get(Calendar.DAY_OF_WEEK) - 1;
switch (weekNumber) {
case 0:
week = WEEK_SUNDAY;
break;
case 1:
week = WEEK_MONDAY;
break;
case 2:
week = WEEK_TUESDAY;
break;
case 3:
week = WEEK_WEDNESDAY;
break;
case 4:
week = WEEK_THURSDAY;
break;
case 5:
week = WEEK_FRIDAY;
break;
case 6:
week = WEEK_SATURDAY;
break;
}
return week;
}
/**
* 日期增加或減少指定天數計算
* @param date 日期
* @param day 天數
* @return
*/
public static Date calDayForDate(Date date, int amount, int fieldType) {
Calendar c = Calendar.getInstance();
c.setTime(date);
switch (fieldType) {
case Calendar.YEAR:
c.add(Calendar.YEAR, amount + 1);
break;
case Calendar.MONTH:
c.add(Calendar.MONTH, amount);
break;
case Calendar.WEEK_OF_YEAR:
c.add(Calendar.WEEK_OF_YEAR, amount);
break;
case Calendar.WEEK_OF_MONTH:
c.add(Calendar.WEEK_OF_MONTH, amount);
break;
case Calendar.DATE:
c.add(Calendar.DATE, amount);
break;
case Calendar.DAY_OF_YEAR:
c.add(Calendar.DAY_OF_YEAR, amount);
break;
case Calendar.HOUR:
c.add(Calendar.HOUR, amount);
break;
case Calendar.MINUTE:
c.add(Calendar.MINUTE, amount);
break;
default:
break;
}
return c.getTime();
}
/**
* 獲取當前日期的前一天
* @return
*/
public static Date getBeforeDay() {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, -1);
return c.getTime();
}
/**
* 獲取當前日期的後一天
* @return
*/
public static Date getLastDay() {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, 1);
return c.getTime();
}
}