package com.ss.mcms.utils;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ss.mcms.common.contants.DateUtilCommonConstants;
import com.ss.mcms.framework.exception.AppException;
/**
*
* 日期转换工具类
*
* @author 13073459
* @see
* @since 1.0.0
*/
public class DateUtil {
static Logger logger = LoggerFactory.getLogger(DateUtil.class);
private static final DateFormat YYYYMMDDHHMMSS_FORMAT = new SimpleDateFormat(
DateUtilCommonConstants.DateConstant.DATEFORMATE_YYYYMMDDHHMMSS);
private static final DateFormat YYYYMMDD_FORMAT = new SimpleDateFormat(
DateUtilCommonConstants.DateConstant.DATEFORMATE_YYYYMMDD);
private static final DateFormat YYYY_MM_DD_FORMAT = new SimpleDateFormat(
DateUtilCommonConstants.DateConstant.DATEFORMATE_YYYY_MM_DD_HHMMSS);
private static final DateFormat CN_TIME_FORMAT = new SimpleDateFormat(
DateUtilCommonConstants.DateConstant.DATEFORMATE_CN_TIME);
private static final DateFormat YYYY_MM_DD = new SimpleDateFormat(
DateUtilCommonConstants.DateConstant.DATEFORMATE_YYYY_MM_DD);
private static final DateFormat YYYYMMDD_SLASH_FORMAT = new SimpleDateFormat(
DateUtilCommonConstants.DateConstant.DATEFORMATE_YYYYMMDD_SLASH);
/**
*
* 功能描述: <br>
* 根据指定格式对日期进行格式化
*
* @param date
* @param format
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static String parseDate(Date date, String format) {
SimpleDateFormat dateformat = new SimpleDateFormat(format);
return dateformat.format(date);
}
/**
*
* 功能描述: <br>
* 获得当前时间
*
* @return
* @see
* @since
*/
public static java.sql.Date getCurDate() {
return getDate(getCurTime());
}
/**
*
* 功能描述: <br>
* 获得当前时间
*
* @return
* @see
* @since
*/
public static Timestamp getCurTime() {
return new Timestamp(Calendar.getInstance().getTime().getTime());
}
public static String getFormatCurDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curdate = getCurDate();
return formatter.format(curdate);
}
/**
* 获取当前日期 yyyy-MM-dd
*
* @return
*/
public static String getCurrentDateString() {
return formateDateStr(new Date());
}
/**
* 获取明天日期 yyyy-MM-dd
*
* @return
*/
public static String getTomorrowDateString() {
return getDateStringDelay(1);
}
/**
* 获取距离今天 delay 天的日期 yyyy-MM-dd
*
* @param delay
* @return
*/
public static String getDateStringDelay(int delay) {
return formateDateStr(DateUtil.addDate(new Date(), delay));
}
/**
*
* 功能描述: <br>
* 〈功能详细描述〉
*
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static String getTime() {
Timestamp time = new Timestamp(Calendar.getInstance().getTime().getTime());
return time.toString();
}
/**
* 得到当前日期
*
* @param type
* :0日期中间横线;1:有横线
* @return
*/
public static String getDate(int type) {
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
String sMonth = String.valueOf(month);
if (sMonth.length() == DateUtilCommonConstants.NumeralConstant.ONE) {
sMonth = DateUtilCommonConstants.StringConstant.ZERO + sMonth;
}
String sDay = String.valueOf(day);
if (sDay.length() == DateUtilCommonConstants.NumeralConstant.ONE) {
sDay = DateUtilCommonConstants.StringConstant.ZERO + sDay;
}
String tDate = String.valueOf(year) + "-" + sMonth + "-" + sDay;
if (type == DateUtilCommonConstants.NumeralConstant.ZERO) {
tDate = String.valueOf(year) + sMonth + sDay;
}
return tDate;
}
/**
* 把yyyy-MM-dd格式的字符串转换成java.util.Date
*
* @param dateStr
* @return
*/
public static java.util.Date getDateOfStr(String dateStr) {
DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date da = null;
try {
da = df.parse(dateStr);
} catch (Exception e) {
logger.debug("异常",e);
}
return da;
/*
* Date date = Date.valueOf(dateStr); return new
* Timestamp(date.getTime());
*/
}
public static java.util.Date getDateFromStr(String dateStr) {
DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date da = null;
try {
da = df.parse(dateStr);
} catch (Exception e) {
logger.debug("异常",e);
}
return da;
}
/**
* 把yyyyMMdd格式的字符串转换成java.util.Date
*
* @param dateStr
* @return
*/
public static Date getDateOfShortStr(String dateStr) {
DateFormat YYYYMMDD_FORMAT_L = new SimpleDateFormat(
DateUtilCommonConstants.DateConstant.DATEFORMATE_YYYYMMDD);
Date da = null;
try {
da = new Date(YYYYMMDD_FORMAT_L.parse(dateStr).getTime());
} catch (Exception e) {
logger.debug("异常",e);
}
return da;
}
/**
* 把日期转换成 yyyyMMdd格式的字符串
*
* @param date
* @return
*/
synchronized public static String getShortStrDate(java.util.Date date) {
return YYYYMMDD_FORMAT.format(date);
}
/**
* 把日期转换成 yyyyMMddHHmmss格式的字符串
*
* @param date
* @return
*/
synchronized public static String getShortStrDateTime(java.util.Date date) {
return YYYYMMDDHHMMSS_FORMAT.format(date);
}
/**
* 把yyyyMMdd格式字符串转换成 java.sql.Date
*
* @param dateStr
* @return
*/
synchronized public static java.sql.Date getSqlDateByShortStr(String dateStr) {
java.sql.Date da = null;
try {
da = new java.sql.Date(YYYYMMDD_FORMAT.parse(dateStr).getTime());
} catch (Exception e) {
logger.debug("异常",e);
}
return da;
}
/**
* 把yyyyMMddHHmmss格式字符串转换成 java.sql.Date
*
* @param dateStr
* @return
*/
synchronized public static java.sql.Date getSqlDateTimeByShortStr(String dateStr) {
java.sql.Date da = null;
try {
da = new java.sql.Date(YYYYMMDDHHMMSS_FORMAT.parse(dateStr).getTime());
} catch (Exception e) {
logger.debug("异常",e);
}
return da;
}
/**
* 字符串转换成 java.sql.Date
*
* @param dateStr
* @return
*/
public static java.sql.Date getSqlDateByPattern(String dateStr, String pattern) {
java.sql.Date da = null;
try {
da = new java.sql.Date(new SimpleDateFormat(pattern).parse(dateStr).getTime());
} catch (Exception e) {
logger.debug("异常",e);
}
return da;
}
/**
* 把yyyy-MM-dd格式的字符串转换成Timestamp
*
* @param dateStr
* @return Timestamp
*/
public static Timestamp getTimeOfDateStr(String dateStr) {
DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.sql.Timestamp time = null;
try {
java.util.Date da = df.parse(dateStr);
time = new java.sql.Timestamp(da.getTime());
} catch (Exception e) {
logger.debug("异常",e);
}
return time;
}
/**
* 把yyyy-MM-dd格式的字符串转换成Timestamp,且该时间是该天的最后时间
*
* @param dateStr
* @return Timestamp
*/
public static Timestamp getTimeEndOfDateStr(String dateStr) {
DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.sql.Timestamp time = null;
try {
java.util.Date da = df.parse(dateStr);
time = new java.sql.Timestamp(da.getTime() + DateUtilCommonConstants.NumeralConstant.TWENTY_FOUR
* DateUtilCommonConstants.NumeralConstant.SIXTY
* DateUtilCommonConstants.NumeralConstant.SIXTY
* DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND
- DateUtilCommonConstants.NumeralConstant.ONE);
} catch (Exception e) {
logger.debug("异常",e);
}
return time;
/*
* Date date = Date.valueOf(dateStr); date.setTime(date.getTime() + 24 *
* 60 * 60 * 1000 - 1); return new Timestamp(date.getTime());
*/
}
/**
* 把yyyy-MM-dd HH:mm:ss.S格式的字符串转换成Timestamp
*
* @param timeStr
* @return Timestamp
*/
public static Timestamp getTimeOfTimeStr(String timeStr) {
DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
java.sql.Timestamp time = null;
try {
java.util.Date da = df.parse(timeStr);
time = new java.sql.Timestamp(da.getTime());
} catch (Exception e) {
logger.debug("异常",e);
}
return time;
/*
* Timestamp time = Timestamp.valueOf(timeStr); return new
* Timestamp(time.getTime());
*/
}
/**
* 把yyyy-MM-dd HH:mm:ss.S格式的字符串转换成Date
*
* @param timeStr
* @return Timestamp
*/
public static Date getDateOfTimeStr(String timeStr) {
DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date da = null;
try {
da = df.parse(timeStr);
} catch (Exception e) {
logger.debug("异常",e);
}
return da;
/*
* Timestamp time = Timestamp.valueOf(timeStr); return new
* Timestamp(time.getTime());
*/
}
/**
* 把pattern格式的字符串转换成Timestamp
*
* @param timeStr
* @return Timestamp
*/
public static Timestamp getTimeOfTimeStr(String timeStr, String pattern) {
DateFormat df = new java.text.SimpleDateFormat(pattern);
java.sql.Timestamp time = null;
try {
java.util.Date da = df.parse(timeStr);
time = new java.sql.Timestamp(da.getTime());
} catch (Exception e) {
logger.debug("异常",e);
}
return time;
}
/**
* 根据Timestamp获得日期
*
* @param time
* @return java.sql.Date
*/
public static java.sql.Date getDate(Timestamp time) {
return (new java.sql.Date(time.getTime()));
}
/**
* 获得对应时间time的相应field的值。 如获得当前时间的分钟,则调用方式如下 getTimeFieldStr(getCurTime(),
* Calendar.MINUTE)
*
* @param time
* @param field
* @return String
*/
public static String getTimeFieldStr(Timestamp time, int field) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date(time.getTime()));
int fieldValue = calendar.get(field);
if (field == Calendar.MONTH)
fieldValue++;
return String.valueOf(fieldValue);
}
/**
* 获得时间time对应的中文日期的字符串
*
* @param time
* @return String -- 如 2003年5月12日
*/
public static String getDateCn(Timestamp time) {
if (time == null)
return null;
String dateCn = getTimeFieldStr(time, Calendar.YEAR) + "年" + getTimeFieldStr(time, Calendar.MONTH)
+ "月" + getTimeFieldStr(time, Calendar.DATE) + "日";
return dateCn;
}
/**
* 获得时间time对应的中文日期的字符串
*
* @param time
* @return String -- 如 2003-5-12
*/
public static String getDateString(Timestamp time, String v) {
String dateCn = getTimeFieldStr(time, Calendar.YEAR) + v + getTimeFieldStr(time, Calendar.MONTH) + v
+ getTimeFieldStr(time, Calendar.DATE);
return dateCn;
}
/**
* 获得日期dateStr是星期几,日期格式是“yyyy-MM-dd”
*
* @param dateStr
* @return String -- 如 星期日
*/
public static String getDayOfWeekCn(String dateStr) {
Calendar cal = Calendar.getInstance();
String weekCn = null;
Timestamp time = getTimeOfDateStr(dateStr);
if (time != null) {
cal.setTime(time);
int day = cal.get(Calendar.DAY_OF_WEEK);
switch (day) {
case DateUtilCommonConstants.NumeralConstant.ONE:
weekCn = "星期日";
break;
case DateUtilCommonConstants.NumeralConstant.TWO:
weekCn = "星期一";
break;
case DateUtilCommonConstants.NumeralConstant.THREE:
weekCn = "星期二";
break;
case DateUtilCommonConstants.NumeralConstant.FOUR:
weekCn = "星期三";
break;
case DateUtilCommonConstants.NumeralConstant.FIVE:
weekCn = "星期四";
break;
case DateUtilCommonConstants.NumeralConstant.SIX:
weekCn = "星期五";
break;
case DateUtilCommonConstants.NumeralConstant.SEVEN:
weekCn = "星期六";
break;
default:
weekCn = "";
}
}
return weekCn;
}
/**
* 将 Timestamp 对应的日期以 "yyyy-MM-dd" 格式返回一个字符串
*
* @param stamp
* @return
*/
public static String formatDate(Timestamp stamp) {
if (stamp == null)
return "";
return new java.sql.Date(stamp.getTime()).toString();
}
/**
* 返回前天,昨天,明天,后天等
*
* @param diffdate
* 于今天相差的天数
* @return
*/
public static String getCustomDate(int diffdate) {
String customDate = "";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curdate = getCurDate();
long myTime = curdate.getTime() + DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND
* DateUtilCommonConstants.NumeralConstant.THREE_THOUSAND_SIX_HUNDRED
* DateUtilCommonConstants.NumeralConstant.TWENTY_FOUR * diffdate;
curdate.setTime(myTime);
customDate = formatter.format(curdate);
return customDate;
}
public static String getFormatDate(java.util.Date date) {
String customDate = "";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
customDate = formatter.format(date);
return customDate;
}
/**
* 停止使用
*
* @param diffdate
* 于今天相差的天数
* @return
*/
@Deprecated
public static String getCustomExDate(String timeStamp, int diffdate) {
String customDate = "";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp temp = getTimeOfTimeStr(timeStamp);
long myTime = temp.getTime() + DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND
* DateUtilCommonConstants.NumeralConstant.THREE_THOUSAND_SIX_HUNDRED
* DateUtilCommonConstants.NumeralConstant.TWENTY_FOUR * diffdate;
temp.setTime(myTime);
customDate = formatter.format(temp);
return customDate;
}
public static String formateDate(java.util.Date date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
if (date != null)
return df.format(date);
return null;
}
/**
* 格式化日期格式返回字符串
*
* @param date
* @param dateFormat
* @return
*/
public static String formateDate(java.util.Date date, DateFormat dateFormat) {
return dateFormat.format(date);
}
/**
* 日期格式 yyyy-MM-dd HH:mm:ss
*
* @param date
* @return
*/
synchronized public static String formateDateString(java.util.Date date) {
if (null == date) {
return "";
}
return formateDate(date, YYYY_MM_DD_FORMAT);
}
/**
* 日期格式 yyyy-MM-dd
*
* @param date
* @return
*/
synchronized public static String formateDateStr(java.util.Date date) {
return formateDate(date, YYYY_MM_DD);
}
/**
* 返回前天,昨天,明天,后天等
*
* @param diffdate
* 于今天相差的天数
* @return
*/
public static String getBeforeDay(int diffdate) {
return getCustomExDate(getTime(), diffdate);
}
/**
* 得到本月的第一天
*
* @return
*/
public static String getMonthFirstDay() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return getFormatDate(calendar.getTime());
}
/**
* 得到本月的最后一天
*
* @return
*/
public static String getMonthLastDay() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return getFormatDate(calendar.getTime());
}
public static int getDaysofMonth(Date date1) throws AppException {
Calendar cld = Calendar.getInstance();
cld.setTime(date1);
cld.set(DateUtilCommonConstants.NumeralConstant.FIVE, DateUtilCommonConstants.NumeralConstant.ONE);
cld.add(DateUtilCommonConstants.NumeralConstant.TWO, DateUtilCommonConstants.NumeralConstant.ONE);
cld.add(DateUtilCommonConstants.NumeralConstant.SIX, -DateUtilCommonConstants.NumeralConstant.ONE);
int days = cld.get(DateUtilCommonConstants.NumeralConstant.FIVE);
return days;
}
public static int getWeeksofMonth(Date date1) throws AppException {
Calendar cld = Calendar.getInstance();
cld.setTime(date1);
cld.set(DateUtilCommonConstants.NumeralConstant.FIVE, DateUtilCommonConstants.NumeralConstant.ONE);
cld.add(DateUtilCommonConstants.NumeralConstant.TWO, DateUtilCommonConstants.NumeralConstant.ONE);
cld.add(DateUtilCommonConstants.NumeralConstant.SIX, -DateUtilCommonConstants.NumeralConstant.ONE);
int weeks = cld.get(DateUtilCommonConstants.NumeralConstant.FOUR);
return weeks;
}
public static boolean isCurrentMonth(Date date1) throws AppException {
Calendar cld = Calendar.getInstance();
int currentYear = cld.get(1);
int currentMonth = cld.get(2);
cld.setTime(date1);
int year = cld.get(1);
int month = cld.get(2);
boolean currentFlag = false;
if (currentYear == year && currentMonth == month)
currentFlag = true;
return currentFlag;
}
public static int getFirstDayofWeek(Date date1) throws AppException {
Calendar cld = Calendar.getInstance();
cld.setTime(date1);
cld.set(DateUtilCommonConstants.NumeralConstant.FIVE, DateUtilCommonConstants.NumeralConstant.ONE);
int dayOfFirstSunday = cld.get(DateUtilCommonConstants.NumeralConstant.SEVEN)
- DateUtilCommonConstants.NumeralConstant.ONE;
return dayOfFirstSunday;
}
public static int getFirstWeekofMonth(Date date1) throws AppException {
Calendar cld = Calendar.getInstance();
cld.setTime(date1);
cld.set(DateUtilCommonConstants.NumeralConstant.FIVE, DateUtilCommonConstants.NumeralConstant.ONE);
int week = cld.get(DateUtilCommonConstants.NumeralConstant.THREE);
return week;
}
public static Date getFirstDayofMonth(Date date1) throws AppException {
Calendar cld = Calendar.getInstance();
cld.setTime(date1);
cld.set(DateUtilCommonConstants.NumeralConstant.FIVE, DateUtilCommonConstants.NumeralConstant.ONE);
return cld.getTime();
}
public static Date getLastDayofMonth(Date date1) throws AppException {
Calendar cld = Calendar.getInstance();
cld.setTime(date1);
cld.set(DateUtilCommonConstants.NumeralConstant.FIVE, DateUtilCommonConstants.NumeralConstant.ONE);
return cld.getTime();
}
public static int getDayofMonth(Date date1) {
Calendar cld = Calendar.getInstance();
cld.setTime(date1);
int day = cld.get(DateUtilCommonConstants.NumeralConstant.FIVE);
return day;
}
public static int getMonth(Date date1) throws AppException {
Calendar cld = Calendar.getInstance();
cld.setTime(date1);
int month = cld.get(DateUtilCommonConstants.NumeralConstant.TWO);
return month;
}
public static int getYear(Date date1) {
Calendar cld = Calendar.getInstance();
cld.setTime(date1);
int year = cld.get(DateUtilCommonConstants.NumeralConstant.ONE);
return year;
}
public static int getDayofWeek(Date date1) throws AppException {
Calendar cld = Calendar.getInstance();
cld.setTime(date1);
int dayOfSunday = cld.get(DateUtilCommonConstants.NumeralConstant.SEVEN);
return dayOfSunday;
}
public static Date getMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 关于DAY_OF_WEEK的说明
// Field number for get and set indicating
// the day of the week. This field takes values
// SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
// and SATURDAY
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return cal.getTime();
}
public static int getCurrentDayofMonth() throws AppException {
Calendar cld = Calendar.getInstance();
int day = cld.get(DateUtilCommonConstants.NumeralConstant.FIVE);
return day;
}
/**
* 日期相加
*
* @param date
* 日期
* @param day
* 天数
* @return 返回相加后的日期
*/
public static java.util.Date addDate(java.util.Date date, int day) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis(getMillis(date) + ((long) day)
* DateUtilCommonConstants.NumeralConstant.TWENTY_FOUR
* DateUtilCommonConstants.NumeralConstant.THREE_THOUSAND_SIX_HUNDRED
* DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND);
return c.getTime();
}
/**
* 日期相加
*
* @param date
* 日期
* @param day
* 天数
* @return 返回相加后的日期
*/
public static java.util.Date addDate(java.util.Date date, double day) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis((long) (getMillis(date) + day * DateUtilCommonConstants.NumeralConstant.TWENTY_FOUR
* DateUtilCommonConstants.NumeralConstant.THREE_THOUSAND_SIX_HUNDRED
* DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND));
return c.getTime();
}
/**
* 分钟相加
*
* @param date
* @param minite
* @return
*/
public static java.util.Date addMinute(java.util.Date date, double minute) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis((long) (getMillis(date) + minute * DateUtilCommonConstants.NumeralConstant.SIXTY
* DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND));
return c.getTime();
}
/**
* 日期相减
*
* @param date
* 日期
* @param date1
* 日期
* @return 返回相减后的日期
*/
public static int diffDate(java.util.Date date, java.util.Date date1) {
return (int) ((getMillis(date) - getMillis(date1)) / (DateUtilCommonConstants.NumeralConstant.TWENTY_FOUR
* DateUtilCommonConstants.NumeralConstant.THREE_THOUSAND_SIX_HUNDRED * DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND));
}
public static int diffDateToHour(Date date, Date date1) {
return (int) ((getMillis(date) - getMillis(date1)) / (DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND
* DateUtilCommonConstants.NumeralConstant.SIXTY * DateUtilCommonConstants.NumeralConstant.SIXTY));
}
/**
* 返回毫秒
*
* @param date
* 日期
* @return 返回毫秒
*/
public static long getMillis(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
public static String formatDateTime(java.sql.Timestamp ts) {
String temp = String.valueOf(ts);
if (!StringUtils.equals(temp, "null")) {
temp = temp.substring(DateUtilCommonConstants.NumeralConstant.ZERO,
DateUtilCommonConstants.NumeralConstant.SIXTEEN);
} else {
temp = "";
}
return temp;
}
/**
* 获取当天日期 字符串形式返回
*
* @return
*/
public static String newDate() {
// DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String date = null;
date = format.format(new java.util.Date());
return date;
}
/**
* 判断是否同年同月
*
* @param t1
* 日期1
* @param t2
* 日期2
* @return
*/
public static boolean isSameMonth(Timestamp t1, Timestamp t2) {
Calendar cal = Calendar.getInstance();
cal.setTime(t1);
int year1 = cal.get(Calendar.YEAR);
int month1 = cal.get(Calendar.MONTH);
cal.setTime(t2);
int year2 = cal.get(Calendar.YEAR);
int month2 = cal.get(Calendar.MONTH);
if (year1 == year2 && month1 == month2) {
return true;
}
return false;
}
/**
* 根据传入的数据获取星期
*
* @param i
* @return
*/
public static String getWeek(int i) {
String result = "";
switch (i) {
case DateUtilCommonConstants.NumeralConstant.ONE:
result = "星期日";
break;
case DateUtilCommonConstants.NumeralConstant.TWO:
result = "星期一";
break;
case DateUtilCommonConstants.NumeralConstant.THREE:
result = "星期二";
break;
case DateUtilCommonConstants.NumeralConstant.FOUR:
result = "星期三";
break;
case DateUtilCommonConstants.NumeralConstant.FIVE:
result = "星期四";
break;
case DateUtilCommonConstants.NumeralConstant.SIX:
result = "星期五";
break;
case DateUtilCommonConstants.NumeralConstant.SEVEN:
result = "星期六";
break;
default:
break;
}
return result;
}
public static String getMonth(int i) {
String result = "";
switch (i) {
case DateUtilCommonConstants.NumeralConstant.ZERO:
result = DateUtilCommonConstants.StringConstant.ONE;
break;
case DateUtilCommonConstants.NumeralConstant.ONE:
result = DateUtilCommonConstants.StringConstant.TWO;
break;
case DateUtilCommonConstants.NumeralConstant.TWO:
result = DateUtilCommonConstants.StringConstant.THREE;
break;
case DateUtilCommonConstants.NumeralConstant.THREE:
result = DateUtilCommonConstants.StringConstant.FOUR;
break;
case DateUtilCommonConstants.NumeralConstant.FOUR:
result = DateUtilCommonConstants.StringConstant.FIVE;
break;
case DateUtilCommonConstants.NumeralConstant.FIVE:
result = DateUtilCommonConstants.StringConstant.SIX;
break;
case DateUtilCommonConstants.NumeralConstant.SIX:
result = DateUtilCommonConstants.StringConstant.SEVEN;
break;
case DateUtilCommonConstants.NumeralConstant.SEVEN:
result = DateUtilCommonConstants.StringConstant.EIGHT;
break;
case DateUtilCommonConstants.NumeralConstant.EIGHT:
result = DateUtilCommonConstants.StringConstant.NINE;
break;
case DateUtilCommonConstants.NumeralConstant.NIEN:
result = DateUtilCommonConstants.StringConstant.TEN;
break;
case DateUtilCommonConstants.NumeralConstant.TEN:
result = DateUtilCommonConstants.StringConstant.ELEVEN;
break;
case DateUtilCommonConstants.NumeralConstant.ELEVEN:
result = DateUtilCommonConstants.StringConstant.TWELVE;
break;
default:
break;
}
return result;
}
/**
* 获取指定格式“星期+月份+日”的字符串
*
* @return
*/
public static String getWeekString(java.util.Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week = cal.get(Calendar.DAY_OF_WEEK);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
return getWeek(week) + " " + getMonth(month) + "." + day;
}
public static Timestamp dateToTimestamp(java.util.Date date) {
if (date != null) {
return new java.sql.Timestamp(date.getTime());
}
return null;
}
/**
* 实现将日期转换成制定格式的Timestamp类型
*
* @param date
* @param format
* @return
*/
public static Timestamp dateChange(java.util.Date date, String format) {
DateFormat df = new SimpleDateFormat(format);
if (date != null) {
try {
return new java.sql.Timestamp(df.parse(df.format(date)).getTime());
} catch (Exception e) {
logger.debug("异常",e);
}
}
return null;
}
/**
* 实现日期和天数相加
*
* @param date
* @param days
* @return 返回相加后的字符串型日期
*/
public static java.util.Date daysAdd(String date, Double days) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date newDate = null;
int day = (int) (double) days;
Calendar cal = Calendar.getInstance();
try {
cal.setTime(format.parse(date));
if (days >= DateUtilCommonConstants.NumeralConstant.ZERO) {
cal.add(Calendar.DAY_OF_YEAR, day);
} else {
return null;
}
newDate = cal.getTime();
// result = format.format(newDate);
} catch (ParseException e) {
logger.debug("异常",e);
}
return newDate;
}
/**
* 实现日期和天数相加
*
* @param date
* @param days
* @return 返回相加后的字符串型日期
*/
public static Date daysAdd(Date date, int days) {
java.util.Date newDate = null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if (days >= 0) {
cal.add(Calendar.DAY_OF_YEAR, days);
} else {
return null;
}
newDate = cal.getTime();
return newDate;
}
/**
* 计算两个日期相减
*
* @param s1
* @param s2
* @return 返回小时
*/
/*public static double getHour(String s1, String s2) {
double quot = DateUtilCommonConstants.NumeralConstant.ZERO;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");// 设定格式
java.sql.Timestamp dateTime1 = null;
java.sql.Timestamp dateTime2 = null;
try {
java.util.Date timeDate1 = dateFormat.parse(s1);
java.util.Date timeDate2 = dateFormat.parse(s2);
dateTime1 = new java.sql.Timestamp(timeDate1.getTime());
dateTime2 = new java.sql.Timestamp(timeDate2.getTime());
quot = dateTime2.getTime() - dateTime1.getTime();
quot = quot / DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND
/ DateUtilCommonConstants.NumeralConstant.SIXTY
/ DateUtilCommonConstants.NumeralConstant.SIXTY;
} catch (ParseException e) {
logger.debug("异常",e);
}
return quot;
}*/
/**
* 计算两个日期相减
*
* @param date1
* @param date2
* @return 返回天数
*/
public static int getDay(java.util.Date date1, java.util.Date date2) {
Long quot = date2.getTime() - date1.getTime();
quot = quot
/ (DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND
* DateUtilCommonConstants.NumeralConstant.SIXTY
* DateUtilCommonConstants.NumeralConstant.SIXTY * DateUtilCommonConstants.NumeralConstant.TWENTY_FOUR)
+ 1;
return quot.intValue();
}
/**
* 将字符串转化为java.sql.Timestamp类型
*
* @param str
* @return
*/
public static java.sql.Timestamp stringToDate(String str) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
java.sql.Timestamp newDate = null;
if (str != null && str.length() == DateUtilCommonConstants.NumeralConstant.TEN) {
try {
newDate = new java.sql.Timestamp(df.parse(str).getTime());
} catch (ParseException e) {
logger.debug("异常",e);
}
}
return newDate;
}
/**
* 对于日期是否是星期六,星期天的判断,并进行处理,决定是否顺延到下一天
*
* @param date
* @param delieverySaturday
* @param delieverySunday
* @return
*/
/*
* public static String filterWeekend(java.util.Date date, String
* delieverySaturday, String delieverySunday) { DateFormat df = new
* SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance();
* cal.setTime(date); int i = cal.get(Calendar.DAY_OF_WEEK); if (i == 7) {
* if ("0".equals(delieverySaturday)) { date = daysAdd(df.format(date),
* 1.0); if ("0".equals(delieverySunday)) date = daysAdd(df.format(date),
* 1.0); } } else if (i == 1) { if ("0".equals(delieverySunday)) date =
* daysAdd(df.format(date), 1.0); } return df.format(date); }
*/
/**
* 计算两个日期相减
*
* @param time1
* @param time2
* @return 返回天数
*/
/*public static double getQuot(Timestamp time1, Timestamp time2) {
if (time1 != null && time2 != null) {
double quot = (double) DateUtilCommonConstants.NumeralConstant.ZERO;
// SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
quot = time1.getTime() - time2.getTime();
quot = quot / DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND
/ DateUtilCommonConstants.NumeralConstant.SIXTY
/ DateUtilCommonConstants.NumeralConstant.SIXTY
/ DateUtilCommonConstants.NumeralConstant.TWENTY_FOUR;
return quot;
} else {
return (double) DateUtilCommonConstants.NumeralConstant.ZERO;
}
}*/
// 判断时间s1是否在时间s2之前
public static boolean compDate(String s1, String s2) {
int day = DateUtilCommonConstants.NumeralConstant.ZERO;
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date time1;
java.util.Date time2;
try {
time1 = sf.parse(s1);
time2 = sf.parse(s2);
day = (int) ((time2.getTime() - time1.getTime()) / 3600 / 24 / 1000);
} catch (ParseException e) {
logger.debug("异常",e);
}
if (day > DateUtilCommonConstants.NumeralConstant.ZERO) {
return true;
} else {
return false;
}
}
// 判断时间s1是否在时间s2之前或者与s2相同
public static boolean compDateAnother(String s1, String s2) {
int day = DateUtilCommonConstants.NumeralConstant.ZERO;
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date time1;
java.util.Date time2;
try {
time1 = sf.parse(s1);
time2 = sf.parse(s2);
day = (int) ((time2.getTime() - time1.getTime()) / 3600 / 24 / 1000);
} catch (ParseException e) {
logger.debug("异常",e);
}
if (day >= DateUtilCommonConstants.NumeralConstant.ZERO) {
return true;
} else {
return false;
}
}
// 判断时间s1是否与s2相同
public static boolean compDateSame(String s1, String s2) {
int day = DateUtilCommonConstants.NumeralConstant.ZERO;
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date time1;
java.util.Date time2;
try {
time1 = sf.parse(s1);
time2 = sf.parse(s2);
day = (int) ((time2.getTime() - time1.getTime()) / 3600 / 24 / 1000);
} catch (ParseException e) {
logger.debug("异常",e);
}
if (day == DateUtilCommonConstants.NumeralConstant.ZERO) {
return true;
} else {
return false;
}
}
// 判断时间s1是否在时间s2之前
public static String compareTo(String s1, String s2) {
int day = DateUtilCommonConstants.NumeralConstant.ZERO;
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date time1;
java.util.Date time2;
try {
time1 = sf.parse(s1);
time2 = sf.parse(s2);
day = (int) ((time2.getTime() - time1.getTime())
/ DateUtilCommonConstants.NumeralConstant.THREE_THOUSAND_SIX_HUNDRED
/ DateUtilCommonConstants.NumeralConstant.TWENTY_FOUR / DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND);
} catch (ParseException e) {
logger.debug("异常",e);
}
if (day > DateUtilCommonConstants.NumeralConstant.ZERO) {
return s2;
} else {
return s1;
}
}
/**
* method 将字符串类型的日期转换为一个timestamp(时间戳记java.sql.Timestamp)
*
* @param dateString
* 需要转换为timestamp的字符串
* @return dataTime timestamp
*/
public static java.sql.Timestamp stringToTime(String dateString) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");// 设定格式
java.sql.Timestamp dateTime = null;
try {
if (dateString != null && dateString.length() > 0) {
java.util.Date timeDate = dateFormat.parse(dateString);// util类型
dateTime = new java.sql.Timestamp(timeDate.getTime());// Timestamp类型,timeDate.getTime()返回一个long型
}
} catch (ParseException e) {
logger.debug("异常",e);
}
return dateTime;
}
/**
* 把日期格式化成中文格式
*/
synchronized public static String parseCnDate(Date date) {
return CN_TIME_FORMAT.format(date);
}
public static Time getTime(Timestamp now) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return Time.valueOf(sdf.format(now));
}
/**
* 把整型转换成时间(如 12345 转换成 01:23:45)
*
* @param intValue
* @return
*/
public static String parseIntValueToTime(Integer intValue) {
String str = "00:00:00";
if (intValue != null) {
String _str = String.valueOf(intValue);
switch (_str.length()) {
case 1:
_str = "00000" + _str;
break;
case 2:
_str = "0000" + _str;
break;
case DateUtilCommonConstants.NumeralConstant.THREE:
_str = "000" + _str;
break;
case DateUtilCommonConstants.NumeralConstant.FOUR:
_str = "00" + _str;
break;
case DateUtilCommonConstants.NumeralConstant.FIVE:
_str = "0" + _str;
break;
case DateUtilCommonConstants.NumeralConstant.SIX:
break;
default:
_str = "000000";
break;
}
str = _str.substring(0, 2) + ":"
+ _str.substring(2, DateUtilCommonConstants.NumeralConstant.FOUR) + ":"
+ _str.substring(DateUtilCommonConstants.NumeralConstant.FOUR);
}
return str;
}
/**
* 返回指定天数的毫秒
*
* @param day
* int类型的天数
* */
public static long getMilliSeconds(int day) {
return (long) day * DateUtilCommonConstants.NumeralConstant.TWENTY_FOUR
* DateUtilCommonConstants.NumeralConstant.THREE_THOUSAND_SIX_HUNDRED
* DateUtilCommonConstants.NumeralConstant.ONE_THOUSAND;
}
/**
* 获取日期的小时
*
* @param date
* 日期
* @return 小时(24小时制)
*/
public static int getHourOfDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.HOUR_OF_DAY);
}
/**
* 设置当前日期的为指定小时的日期 2011-11-24 14:23:00 ---18---2011-11-24 18:23:00
*
* @param date
* 日期
* @param hour
* 小时(24小时)
* @return
*/
public static Date setHourOfDate(Date date, int hour) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, hour);
return cal.getTime();
}
//把yyyymmdd转成yyyy-MM-dd格式
public static String formatDate(String str){
SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sf2 =new SimpleDateFormat("yyyy-MM-dd");
String sfstr = "";
try {
sfstr = sf2.format(sf1.parse(str));
} catch (ParseException e) {
logger.debug("异常",e);
}
return sfstr;
}
/**
* 返回当前时间的自定义格式String
*
* @param dateFormat
* @return 自定义格式的日期
*
* 自定义支持的格式有: yyyy-MM-dd HH:mm:ss yyyy年MM月dd日 HH时mm分ss秒 yyyy年MM月dd日
* HH时mm分 yyyy-MM-dd HH:mm yyyyMMddHH:mm:ss yyyy-MM-dd yyyyMMdd
* HHmmss yyyy年MM月dd日 HH:mm:ss" HH时mm分ss秒 ......(很多)
*/
public static String getDefinableTime(String dateFormat) {
if (null == dateFormat || "".equals(dateFormat.trim())) {
return "";
}
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 返回指定时间的自定义格式String
*
* @param dateFormat
* @return 自定义格式的日期
*
* 自定义支持的格式有: yyyy-MM-dd HH:mm:ss yyyy年MM月dd日 HH时mm分ss秒 yyyy年MM月dd日
* HH时mm分 yyyy-MM-dd HH:mm yyyyMMddHH:mm:ss yyyy-MM-dd yyyyMMdd
* HHmmss yyyy年MM月dd日 HH:mm:ss" HH时mm分ss秒 ......(很多)
*/
public static String getDefinableTime(Date date, String dateFormat) {
if (null == date || null == dateFormat || "".equals(dateFormat.trim())) {
return "";
}
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
String dateString = formatter.format(date);
return dateString;
}
/**
* 获取YYYY/MM/DD字符串
*
* @param date
* 待格式化日期
* @return YYYY/MM/DD字符串
*/
synchronized public static String getYYMMDDWidthSlash(Date date) {
return YYYYMMDD_SLASH_FORMAT.format(date);
}
/**
* 根据传入的日期格式(yyyy-MM-dd HH:mm:ss)返回yyyyMMddHHmmss格式日期
*
* @param date
* @return yyyyMMddHHmmss日期
*/
public static String getDefinableDateStr(String date) {
if (date == null) {
return "";
}
return getDefinableTime(getDateOfTimeStr(date), "yyyyMMddHHmmss");
}
/**
* 后n个工作日
*
* @param date
* //传入时间
* @param nextWorkDay
* //后退工作日时间
* @param chinaHoliday
* // 传统节假日(例如春节)
* @param normalDay
* // 调休日期
*
* @return
*/
public static String nextWorkDay(String date, int nextWorkDay, List<?> chinaHoliday, List<?> normalDay) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat holidaysdf = new SimpleDateFormat("MM-dd");
Calendar cal = Calendar.getInstance();
int count = DateUtilCommonConstants.NumeralConstant.ZERO;
try {
Date startDate = sdf.parse(date);
cal.setTime(startDate);
while (count < nextWorkDay) {
cal.add(Calendar.DATE, 1);
date = sdf.format(cal.getTime());
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
if (normalDay.contains(date)) {
count++;
continue;
}
} else {
if (!holidayList(holidaysdf.format(cal.getTime())) && !chinaHoliday.contains(date)) {
count++;
}
}
}
} catch (ParseException e) {
logger.debug("异常",e);
}
return date;
}
/**
* 前n个工作日
*
* @param date
* //传入时间
* @param nextWorkDay
* //前进工作日时间
* @param chinaHoliday
* // 传统节假日(例如春节)
* @param normalDay
* // 调休日期
*
* @return
*/
public static String preWorkDay(String date, int preWorkDay, List<?> chinaHoliday, List<?> normalDay) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat holidaysdf = new SimpleDateFormat("MM-dd");
Calendar cal = Calendar.getInstance();
int count = preWorkDay;
try {
Date startDate = sdf.parse(date);
cal.setTime(startDate);
while (count > DateUtilCommonConstants.NumeralConstant.ZERO) {
cal.add(Calendar.DATE, -1);
date = sdf.format(cal.getTime());
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
if (normalDay.contains(date)) {
count--;
continue;
}
} else {
if (!holidayList(holidaysdf.format(cal.getTime())) && !chinaHoliday.contains(date)) {
count--;
}
}
}
} catch (ParseException e) {
logger.debug("异常",e);
}
return date;
}
/**
*
* 功能描述: <br>
* 法定假日,元旦,五一和国庆
*
* @param findDate
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static boolean holidayList(String findDate) {
List<String> ls = new ArrayList<String>();
ls.add("01-01");
ls.add("05-01");
ls.add("10-01");
ls.add("10-02");
ls.add("10-03");
if (ls.contains(findDate)) {
return true;
} else {
return false;
}
}
/**
*
* 功能描述: <br>
* 生成一个 18 位的 yyyyMMddHHmmss.SSS 格式的日期字符串
*
* @param date
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static String genTimeStampString(Date date) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return df.format(date);
}
/**
*
* 功能描述: <br>
* date 转 YYYYMMDD 字符串
*
* @param date
* @return
* @see
* @since 2.0
*/
public static String date2YYYYMMDDStr(Date date) {
DateFormat YYYYMMDD_FORMAT_L = new SimpleDateFormat("yyyyMMdd");
return YYYYMMDD_FORMAT_L.format(date);
}
/**
*
* 功能描述: <br>
* 根据指定日期,获取年,月,日;并以数组形式返回
*
* @param date
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static String[] getWeekStringArray(Date date) {
String[] ymd = new String[DateUtilCommonConstants.NumeralConstant.THREE];
Calendar cal = Calendar.getInstance();
cal.setTime(date);
ymd[0] = String.valueOf(cal.get(Calendar.YEAR));
ymd[1] = String.valueOf(cal.get(Calendar.MONTH) + 1);
ymd[2] = String.valueOf(cal.get(Calendar.DATE));
return ymd;
}
/**
*
* 功能描述: <br>
* DB2日期格式截取为 YYYY-MM-DD HH:mm:ss 形式
*
* @param date
* @return
* @see
* @since 3.1
*/
public static String formatDBDateStr2Str(String date) {
String retStr = "";
if (StringUtils.isNotBlank(date)) {
retStr = date.substring(DateUtilCommonConstants.NumeralConstant.ZERO, date.lastIndexOf("."));
}
return retStr;
}
public static String intToString(int n, int l) {
DecimalFormat decimalFormat = new DecimalFormat(DateUtil.initString('0', l));
return decimalFormat.format(n);
}
/**
* 返回指定长度的,由指定字符组成的字符串(相当于c语言的memset)
*
* @param ch
* @param length
* @return
*/
public static String initString(char ch, int length) {
char[] chars = new char[length];
if (length < 0) {
return "";
}
for (int i = 0; i < length; i++) {
chars[i] = ch;
}
return new String(chars);
}
/**
*
* 功能描述: 获取day天的日期<br>
* 〈功能详细描述〉
*
* @param date
* @param day
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static Date getAfterDate(Date date, int day) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.DATE, day);
return calendar.getTime();
}
/**
*
* 功能描述: <br>
* 〈功能详细描述〉
*
* @param dateStr
* @return
* @throws ParseException
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static Date getDateFromString(String dateStr) throws AppException {
String date = dateStr.replaceAll("-", "");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
sdf.setLenient(false);
try {
return sdf.parse(date);
} catch (Exception e) {
logger.debug("e",e);
throw new AppException("日期格式不正确!");
}
}
/**
*
* 功能描述: 计算两个日期之间的天数<br>
* 〈功能详细描述〉
*
* @param early
* @param late
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static final int calDaysBetween(Date early, Date late) {
return (int) ((DateUtil.getCalendar(late).getTime().getTime() - DateUtil.getCalendar(early)
.getTime().getTime()) / (1000 * 60 * 60 * 24));
}
/**
*
* 功能描述: 返回Calendar的日期格式<br>
* 〈功能详细描述〉
*
* @param date
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static Calendar getCalendar(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar;
}
/**
* 获取当天日期 字符串形式返回
*
* @return
*/
public static String newDate(String dateFormat) {
DateFormat format = new SimpleDateFormat(dateFormat);
String date = format.format(new java.util.Date());
return date;
}
public static Date getStringsToDate(String dateStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date da = null;
try {
da = sdf.parse(dateStr);
} catch (Exception e) {
logger.debug("异常",e);
}
return da;
}
/**
* 功能描述:返回当前日期是否是月末日期 <br>
* 〈功能详细描述〉
*
* @param date
* @return
* @throws ParseException
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static boolean getAccruedDate(String date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(dateFormat.parse(date));
} catch (ParseException e) {
logger.debug("异常",e);
}
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime()).equals(date);
}
/**
* 功能描述:返回当前日期是否是当月1号 <br>
* 〈功能详细描述〉
*
* @param date
* @return
* @throws ParseException
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static boolean getFirstMonthDate(String date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(dateFormat.parse(date));
} catch (ParseException e) {
logger.debug("异常",e);
}
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
return dateFormat.format(calendar.getTime()).equals(date);
}
/**
* 获取前一天的日期
*
* @param holiday 指定入参日期
* @return
*/
public static String queryBeforeDate(Date holiday) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFm = new SimpleDateFormat("yyyyMMdd");
calendar.setTime(holiday);
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date beforeDate = calendar.getTime();
return dateFm.format(beforeDate);
}
/**
* 功能描述: <br>
* 〈功能详细描述:获取后一天的日期〉
*
* @param holiday
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static String queryAfterDate(Date holiday) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFm = new SimpleDateFormat("yyyyMMdd");
calendar.setTime(holiday);
calendar.add(Calendar.DAY_OF_MONTH, +1);
Date afterDate = calendar.getTime();
return dateFm.format(afterDate);
}
/**
*
* 功能描述: <br>
* 〈获取前一天的日期〉
*
* @param dateStr
* @param formatStr
* @return
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static Date queryBeforeDate(String dateStr,String formatStr) {
Date beforeDate = new Date();
try {
Date date = null;
Calendar calendar = Calendar.getInstance();
DateFormat dateFormat= new SimpleDateFormat(formatStr);
date = new Date(dateFormat.parse(dateStr).getTime());
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
beforeDate = calendar.getTime();
} catch (ParseException e) {
logger.debug("异常",e);
}
return beforeDate;
}
/**
* 功能描述:由字符串类型转到yyyy-MM-dd格式日期 <br>
* 〈功能详细描述〉
*
* @param dateStr
* @return
* @throws AppException
* @throws Exception
* @throws Exception
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static Date getStringToDate(String dateStr) throws AppException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
throw new AppException("转换日期格式异常");
}
}
}