package com.yss.framework.util;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Pattern;
import com.yss.framework.api.exception.YssException;
public final class DateUtil {
// 格式:年-月-日 小时:分钟:秒
public static final String FORMAT_ONE = "yyyy-MM-dd HH:mm:ss";
// 格式:年-月-日 小时:分钟
public static final String FORMAT_TWO = "yyyy-MM-dd HH:mm";
// 格式:年月日 小时分钟秒
public static final String FORMAT_THREE = "yyyyMMdd-HHmmss";
// 格式:年-月-日
public static final String LONG_DATE_FORMAT = "yyyy-MM-dd";
// 格式:月-日
public static final String SHORT_DATE_FORMAT = "MM-dd";
// 格式:小时:分钟:秒
public static final String LONG_TIME_FORMAT = "HH:mm:ss";
// 格式:年-月
public static final String MONTG_DATE_FORMAT = "yyyy-MM";
// 年的加减
public static final int SUB_YEAR = Calendar.YEAR;
// 月加减
public static final int SUB_MONTH = Calendar.MONTH;
// 天的加减
public static final int SUB_DAY = Calendar.DATE;
// 小时的加减
public static final int SUB_HOUR = Calendar.HOUR;
// 分钟的加减
public static final int SUB_MINUTE = Calendar.MINUTE;
// 秒的加减
public static final int SUB_SECOND = Calendar.SECOND;
static final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四",
"星期五", "星期六" };
@SuppressWarnings("unused")
private static final SimpleDateFormat timeFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
public DateUtil() {
}
/**
* 把符合日期格式的字符串转换为日期类型
*
* @param dateStr
* @return
*/
public static java.util.Date stringtoDate(String dateStr, String format) {
Date d = null;
SimpleDateFormat formater = new SimpleDateFormat(format);
try {
formater.setLenient(false);
d = formater.parse(dateStr);
} catch (Exception e) {
// log.error(e);
d = null;
}
return d;
}
/**
* 把符合日期格式的字符串转换为日期类型
*/
public static java.util.Date stringtoDate(String dateStr, String format,
ParsePosition pos) {
Date d = null;
SimpleDateFormat formater = new SimpleDateFormat(format);
try {
formater.setLenient(false);
d = formater.parse(dateStr, pos);
} catch (Exception e) {
d = null;
}
return d;
}
/**
* 把日期转换为字符串
*
* @param date
* @return
*/
public static String dateToString(java.util.Date date, String format) {
String result = "";
SimpleDateFormat formater = new SimpleDateFormat(format);
try {
result = formater.format(date);
} catch (Exception e) {
// log.error(e);
}
return result;
}
/**
* 获取当前时间的指定格式
*
* @param format
* @return
*/
public static String getCurrDate(String format) {
return dateToString(new Date(), format);
}
/**
*
* @param dateStr
* @param amount
* @return
*/
public static String dateSub(int dateKind, String dateStr, int amount) {
Date date = stringtoDate(dateStr, FORMAT_ONE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(dateKind, amount);
return dateToString(calendar.getTime(), FORMAT_ONE);
}
/**
* 两个日期相减
*
* @param firstTime
* @param secTime
* @return 相减得到的秒数
*/
public static long timeSub(String firstTime, String secTime) {
long first = stringtoDate(firstTime, FORMAT_ONE).getTime();
long second = stringtoDate(secTime, FORMAT_ONE).getTime();
return (second - first) / 0;
}
/**
* 获得某月的天数
*
* @param year
* int
* @param month
* int
* @return int
*/
public static int getDaysOfMonth(String year, String month) {
int days = 0;
if (month.equals("1") || month.equals("3") || month.equals("5")
|| month.equals("7") || month.equals("8") || month.equals("10")
|| month.equals("12")) {
days = 31;
} else if (month.equals("4") || month.equals("6") || month.equals("9")
|| month.equals("11")) {
days = 30;
} else {
if ((Integer.parseInt(year) % 4 == 0 && Integer.parseInt(year) % 100 != 0)
|| Integer.parseInt(year) % 400 == 0) {
days = 29;
} else {
days = 28;
}
}
return days;
}
/**
* 获取某年某月的天数
*
* @param year
* int
* @param month
* int 月份[1-12]
* @return int
*/
public static int getDaysOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* 获得当前日期
*
* @return int
*/
public static int getToday() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.DATE);
}
/**
* 获得当前月份
*
* @return int
*/
public static int getToMonth() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.MONTH) + 1;
}
/**
* 获得当前年份
*
* @return int
*/
public static int getToYear() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.YEAR);
}
/**
* 返回日期的天
*
* @param date
* Date
* @return int
*/
public static int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DATE);
}
/**
* 返回日期的年
*
* @param date
* Date
* @return int
*/
public static int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
/**
* 返回日期的月份,1-12
*
* @param date
* Date
* @return int
*/
public static int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1;
}
/**
* 计算两个日期相差的天数,如果date2 > date1 返回正数,否则返回负数
*
* @param date1
* Date
* @param date2
* Date
* @return long
*/
public static long dayDiff(Date date1, Date date2) {
return (date2.getTime() - date1.getTime()) / 86400000;
}
/**
* 比较两个日期的年差
*
* @param befor
* @param after
* @return
*/
public static int yearDiff(String before, String after) {
Date beforeDay = stringtoDate(before, LONG_DATE_FORMAT);
Date afterDay = stringtoDate(after, LONG_DATE_FORMAT);
return getYear(afterDay) - getYear(beforeDay);
}
/**
* 比较指定日期与当前日期的差
*
* @param befor
* @param after
* @return
*/
public static int yearDiffCurr(String after) {
Date beforeDay = new Date();
Date afterDay = stringtoDate(after, LONG_DATE_FORMAT);
return getYear(beforeDay) - getYear(afterDay);
}
/**
* 比较指定日期与当前日期的差
*
* @param before
* @return
* @author chenyz
*/
public static long dayDiffCurr(String before) {
Date currDate = DateUtil.stringtoDate(currDay(), LONG_DATE_FORMAT);
Date beforeDate = stringtoDate(before, LONG_DATE_FORMAT);
return (currDate.getTime() - beforeDate.getTime()) / 00;
}
/**
* 获取每月的第一周
*
* @param year
* @param month
* @return
* @author chenyz
*/
public static int getFirstWeekdayOfMonth(int year, int month) {
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.SATURDAY); // 鏄熸湡澶╀负绗竴澶?
c.set(year, month - 1, 1);
return c.get(Calendar.DAY_OF_WEEK);
}
/**
* 获取每月的最后一周
*
* @param year
* @param month
* @return
* @author chenyz
*/
public static int getLastWeekdayOfMonth(int year, int month) {
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.SATURDAY); // 鏄熸湡澶╀负绗竴澶?
c.set(year, month - 1, getDaysOfMonth(year, month));
return c.get(Calendar.DAY_OF_WEEK);
}
/**
* 获取指定日期所属年份的第一天,即当年的1月1日
*
* @param baseDate
* @return
*/
public static Date getFirstDay(Date baseDate) {
Date firstDate = null;
Calendar calendar = null;
if (null != baseDate) {
calendar = convertToCalendar(baseDate);
calendar.set(calendar.get(Calendar.YEAR), 0, 1);
firstDate = calendar.getTime();
}
return firstDate;
}
/**
* 获得当前日期字符串,格式"yyyy-MM-dd HH:mm:ss"
*
* @return
*/
public static String getCurrent() {
String temp_str = "";
Date dt = new Date();
//链?悗镄刟a琛ㄧず钬滀笂鍗堚?鎴栤?涓嫔崃钬? HH琛ㄧず24灏忔椂鍒? 濡傛灉鎹㈡垚hh琛ㄧず12灏忔椂鍒?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
temp_str = sdf.format(dt);
return temp_str;
}
/**
* 获得当前日期字符串,格式"yyyy-MM-dd HH:mm:ss"
*
* @return
*/
public static String getNow() {
Calendar today = Calendar.getInstance();
return dateToString(today.getTime(), FORMAT_ONE);
}
/**
* 获得当前日期字符串,格式"yyyy-MM-dd HH:mm:ss"
*
* @return
*/
public static String getNow(String formate) {
Calendar today = Calendar.getInstance();
return dateToString(today.getTime(), formate);
}
/**
* 根据生日获取星座
*
* @param birth
* YYYY-mm-dd
* @return
*/
public static String getAstro(String birth) {
if (!isDate(birth)) {
birth = "0" + birth;
}
if (!isDate(birth)) {
return "";
}
int month = Integer.parseInt(birth.substring(birth.indexOf("-") + 1,
birth.lastIndexOf("-")));
int day = Integer.parseInt(birth.substring(birth.lastIndexOf("-") + 1));
String s = "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
int[] arr = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };
int start = month * 2 - (day < arr[month - 1] ? 2 : 0);
return s.substring(start, start + 2) + "座";
}
/**
* 判断日期是否有效,包括闰年的情况
*
* @param date
* YYYY-mm-dd
* @return
*/
public static boolean isDateLumbda(String date) {
StringBuffer reg = new StringBuffer(
"^((\\d{2}(([68][])|([79][26]))-?((((0?");
reg.append("[78])|(1[02]))-?((0?[1-9])|([1-2][0-9])|(3[01])))");
reg.append("|(((0?[])|(11))-?((0?[1-9])|([1-2][0-9])|(30)))|");
reg.append("(0?2-?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([68][12");
reg.append("79])|([79][89]))-?((((0?[78])|(1[02]))");
reg.append("-?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[])|(11))");
reg.append("-?((0?[1-9])|([1-2][0-9])|(30)))|(0?2-?((0?[");
reg.append("1-9])|(1[0-9])|(2[0-8]))))))");
Pattern p = Pattern.compile(reg.toString());
return p.matcher(date).matches();
}
/**
* 是否可以转换成日期,仅解析用/-.间隔的日期 dDate用于返回日期,改写dDate中已经存在的日期
* @param sDate
* @param dDate
* @return 是否可以转换
*/
public static boolean isDate(String sDate, Date dDate)
{
long ltmp = 0; //
try {
ltmp = toDate(sDate).getTime();
if (null != dDate) {
dDate.setTime(ltmp);
} // end if
return true;
} catch (Exception ye) {
return false;
}
}
/**
* 判断是否为日期
* @param sDate
* @return 是否为日期
*/
public static final boolean isDate(String sDate)
{
return isDate(sDate, null);
}
/**
* 类似vb的CDate函数,自动分析sDate,如格式正常,返回日期,否则报错。<br>
* 注意这里只能处理单纯日期,不处理时间,年份正常范围在0-99和1000-9999 仅解析用/-.间隔的日期<br>
* 修改采用"."为分隔符的日期字符串的BUG,因为"."作为正则表达式的特殊字符,故做分隔符时用"\."处理 by leeyu 20111124<br>
* @param sDate
* @return 返回的日期
* @throws YssException
*/
public static Date toDate(String sDate) throws Exception
{
int jj = 0; //
char ss =0;
char cc = 0;
String[] sss = { "-", "/", "\\." };
String[] result = null; //
int kk = 0; //
int mm = 0; //
final String emsg = "非法日期格式!"; //
GregorianCalendar cl = null; //
if(sDate == null ){
throw new Exception("参数为空,转换失败!");
}
// 将日期后面的时间去掉,这里只转日期部分byleeyu20120717
if(sDate.length()>10){
sDate = sDate.substring(0,10);
}
// 检查分隔符
for (jj = 0; jj < sss.length; jj++) {
if (sDate.split(sss[jj]).length > 1) {
break;
} // end if
} // end for
if (jj >= sss.length) {
throw new Exception(emsg);
} // end if
ss = sss[jj].length()== 2 ? sss[jj].charAt(1) : sss[jj].charAt(0);
// 检查数字有效性即除了数字和分隔符,不应该再包括其它字符
for (int i = 0; i < sDate.length(); i++) {
cc = sDate.charAt(i);
if (cc != ss && (cc < '0' || cc > '9')) {
throw new Exception(emsg);
} // end if
} // end for
// 劈开,获取3个数字
result = sDate.split(sss[jj], -1); // 检查全部,包括空的元素,用0会忽略空
if (3 != result.length) {
throw new Exception(emsg);
} // end if
jj = Integer.parseInt(result[0]);
kk = Integer.parseInt(result[1]);
mm = Integer.parseInt(result[2]);
// 判断是否符合一种日期格式
// 1、y/M/d格式
if (isValidDate(jj, kk, mm)) {
cl = new GregorianCalendar(jj < 30 ? jj + 2000
: (jj <= 99 ? jj + 1900 : jj), kk - 1, mm);
} else {
if (mm < 30) {
mm += 2000;
} else if (mm <= 99) {
mm += 1900;
// 2、M/d/y格式
} // end if
if (isValidDate(mm, jj, kk)) {
cl = new GregorianCalendar(mm, jj - 1, kk);
// 3、d/M/y格式
} else if (isValidDate(mm, kk, jj)) {
cl = new GregorianCalendar(mm, kk - 1, jj);
} else {
throw new Exception(emsg);
} // end if
} // end if
return cl.getTime();
}
/**
* 判断年月日是否在正常范围 年份正常范围在0-99和1000-9999
*/
public static boolean isValidDate(int year, int month, int day)
{
GregorianCalendar cl;
if (year < 0 || (year > 99 && (year < 1000 || year > 9999))) {
return false;
} // end if
if (year < 30) {
year += 2000;
} else if (year <= 99) {
year += 1900;
} // end if
if (month < 1 || month > 12) {
return false;
} // end if
cl = new GregorianCalendar(year, month - 1, 1); // 参数月份从0开始所以减一
if (day < cl.getActualMinimum(Calendar.DAY_OF_MONTH)
|| day > cl.getActualMaximum(Calendar.DAY_OF_MONTH)) {
return false;
} // end if
return true;
}
/**
* 判断指定日期是否是当月最后一个自然日
*
* @param date
* 需要判断的日期
* @return 是当月最后一个自然日,返回真,反之为假
*/
public static boolean isLastNatureDayOnMonth(Date date) {
boolean result = false;
Calendar calendar = null;
if (null != date) {
// 转换日期格式
calendar = convertToCalendar(date);
// 如果日期是本月最大天数,则为最后一个自然日
if (calendar.get(Calendar.DAY_OF_MONTH) == calendar.getActualMaximum(Calendar.DATE)) {
result = true;
}
}
return result;
}
/**
* 判断指定日期是否是日期所在周的最后一个自然日
*
* @param date
* 日期
* @return
*/
public static boolean isLastNatureDayOnWeek(Date date) {
boolean result = true;
Calendar calendar = null;
if (null != date) {
calendar = convertToCalendar(date);
// 如果当前日期是周末的话
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
result = true;
}
}
return result;
}
/**
* 判断指定日期是否是日期所在年的最后一个自然日
*
* @param date
* 日期
* @return
*/
public static boolean isLastNatureDayOnYear(Date date) {
boolean result = false;
Calendar calendar = null;
if (null != date) {
calendar = convertToCalendar(date);
// 如果当前日期是年末的话
if (calendar.get(Calendar.DAY_OF_YEAR) == calendar.getActualMaximum(Calendar.DAY_OF_YEAR)) {
result = true;
}
}
return result;
}
/**
* 转换Date类型为Caleadar类型
* @param date
* @return calendar
*/
public static Calendar convertToCalendar(Date date) {
Calendar calendar = null;
if (null != date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
}
return calendar;
}
/**
* 取得指定日期过 months 月后的日期 (当 months 为负数表示指定月之前);
*
* @param date
* 日期 为null时表示当天
* @param month
* 相加(相减)的月数
*/
public static Date nextMonth(Date date, int months) {
Calendar cal = Calendar.getInstance();
if (date != null) {
cal.setTime(date);
}
cal.add(Calendar.MONTH, months);
return cal.getTime();
}
/**
* 取得指定日期过 day 天后的日期 (当 day 为负数表示指日期之前);
*
* @param date
* 日期 为null时表示当天
* @param month
* 相加(相减)的月数
*/
public static Date nextDay(Date date, int day) {
Calendar cal = Calendar.getInstance();
if (date != null) {
cal.setTime(date);
}
cal.add(Calendar.DAY_OF_YEAR, day);
return cal.getTime();
}
/**
* 取得距离今天 day 日的日期
*
* @param day
* @param format
* @return
* @author chenyz
*/
public static String nextDay(int day, String format) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_YEAR, day);
return dateToString(cal.getTime(), format);
}
/**
* 取得指定日期过 day 周后的日期 (当 day 为负数表示指定月之前)
*
* @param date
* 日期 为null时表示当天
*/
public static Date nextWeek(Date date, int week) {
Calendar cal = Calendar.getInstance();
if (date != null) {
cal.setTime(date);
}
cal.add(Calendar.WEEK_OF_MONTH, week);
return cal.getTime();
}
/**
* 获取当前的日期(yyyy-MM-dd)
*/
public static String currDay() {
return DateUtil.dateToString(new Date(), DateUtil.LONG_DATE_FORMAT);
}
/**
* 获取昨天的日期
*
* @return
*/
public static String befoDay() {
return befoDay(DateUtil.LONG_DATE_FORMAT);
}
/**
* 获取昨天的日期
*
* @return
*/
public static String today() {
return DateUtil.dateToString(new Date(), DateUtil.LONG_DATE_FORMAT);
}
/**
* 根据时间类型获取昨天的日期
*
* @param format
* @return
* @author chenyz
*/
public static String befoDay(String format) {
return DateUtil.dateToString(DateUtil.nextDay(new Date(), -1), format);
}
/**
* 获取明天的日期
*/
public static String afterDay() {
return DateUtil.dateToString(DateUtil.nextDay(new Date(), 1),
DateUtil.LONG_DATE_FORMAT);
}
/**
* 取得当前时间距离0/1/1的天数
*
* @return
*/
public static int getDayNum() {
int daynum = 0;
GregorianCalendar gd = new GregorianCalendar();
Date dt = gd.getTime();
GregorianCalendar gd1 = new GregorianCalendar(0, 1, 1);
Date dt1 = gd1.getTime();
daynum = (int) ((dt.getTime() - dt1.getTime()) / (24 * 60 * 60 * 0));
return daynum;
}
/**
* getDayNum的逆方法(用于处理Excel取出的日期格式数据等)
*
* @param day
* @return
*/
public static Date getDateByNum(int day) {
GregorianCalendar gd = new GregorianCalendar(0, 1, 1);
Date date = gd.getTime();
date = nextDay(date, day);
return date;
}
/** 针对yyyy-MM-dd HH:mm:ss格式,显示yyyymmdd */
public static String getYmdDateCN(String datestr) {
if (datestr == null)
return "";
if (datestr.length() < 10)
return "";
StringBuffer buf = new StringBuffer();
buf.append(datestr.substring(0, 4)).append(datestr.substring(5, 7))
.append(datestr.substring(8, 10));
return buf.toString();
}
/**
* 获取本月第一天
*
* @param format
* @return
*/
public static String getFirstDayOfMonth(String format) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 1);
return dateToString(cal.getTime(), format);
}
/**
* 根据指定日期获取当月的第一个自然日,即当月第一天
*
* @param date
* 指定日期
* @return 当月第一天
*/
public static Date getFirstNatureDayOnMonth(Date date) {
Date lastDate = null;
Calendar calendar = null;
if (null != date) {
// 转换日期格式
calendar = convertToCalendar(date);
// 获取当期月份的最小天数
int days = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
// 按yyyy-MM-dd格式返回所需日期
calendar.set(Calendar.DAY_OF_MONTH, days);
lastDate = calendar.getTime();
}
return lastDate;
}
/**
* 根据指定日期获取当月的最后一个自然日,即当月最后一天
*
* @param date
* 需要判断的日期
* @return 返回当月最后一个自然日
*/
public static Date getLastNatureDayOnMonth(Date date) {
Date lastDate = null;
Calendar calendar = null;
if (null != date) {
// 转换日期格式
calendar = convertToCalendar(date);
// 获取当期月份的最大天数
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
// 按yyyy-MM-dd格式返回所需日期
calendar.set(Calendar.DAY_OF_MONTH, days);
lastDate = calendar.getTime();
}
return lastDate;
}
/**
* 获取日期所在周的第一个自然日
*
* @param date
* 基准日期
* @return
*/
public static Date getFirstNatureDayOnWeek(Date date) {
Date firstDate = null;
Calendar calendar = null;
if (null != date) {
calendar = convertToCalendar(date);
// 获取当期星期的最小天数
int days = calendar.getActualMinimum(Calendar.DAY_OF_WEEK);
// 按yyyy-MM-dd格式返回所需日期
calendar.set(Calendar.DAY_OF_WEEK, days);
firstDate = calendar.getTime();
}
return firstDate;
}
/**
* 获取日期所在周的最后一个自然日
*
* @param date
* 基准日期
* @return
*/
public static Date getLastNatureDayOnWeek(Date date) {
Date lastDate = null;
Calendar calendar = null;
if (null != date) {
calendar = convertToCalendar(date);
// 获取当期星期的最小天数
int days = calendar.getActualMaximum(Calendar.DAY_OF_WEEK);
// 按yyyy-MM-dd格式返回所需日期
calendar.set(Calendar.DAY_OF_WEEK, days);
lastDate = calendar.getTime();
}
return lastDate;
}
/**
* 获取日期所在半年的第一个自然日
*
* @param date
* 基准日期
* @return
*/
public static Date getFirstNatureDayOnHalfYear(Date date) {
Date lastDate = null;
Calendar calendar = null;
if (null != date) {
calendar = convertToCalendar(date);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
if (currentMonth >= 1 && currentMonth <= 6) {
calendar.set(Calendar.MONTH, 0);
} else {
calendar.set(Calendar.MONTH, 6);
}
calendar.set(Calendar.DATE, 1);
lastDate = calendar.getTime();
}
return lastDate;
}
/**
* 获取日期所在周的第一个自然日
*
* @param date
* 基准日期
* @return
*/
public static Date getFirstNatureDayOnYear(Date date) {
Date firstDate = null;
Calendar calendar = null;
if (null != date) {
calendar = convertToCalendar(date);
// 获取当前年的最小天数
int days = calendar.getActualMinimum(Calendar.DAY_OF_YEAR);
// 按yyyy-MM-dd格式返回所需日期
calendar.set(Calendar.DAY_OF_YEAR, days);
firstDate = calendar.getTime();
}
return firstDate;
}
/**
* 根据指定日期获取当前的最后一个自然日
*
* @param date
* @return 返回当年最后一个自然日
*/
public static Date getLastNatureDayOnYear(Date date) {
Date lastDate = null;
Calendar calendar = null;
if (null != date) {
calendar = convertToCalendar(date);
// 获取当前年的最大天数
int days = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
// 按yyyy-MM-dd格式返回所需日期
calendar.set(Calendar.DAY_OF_YEAR, days);
lastDate = calendar.getTime();
}
return lastDate;
}
/**
* 根据指定日期获取该日期所在季度的最后一个自然日
*
* @param date
* 需要判断的日期
* @return 返回当前季度最后一个自然日
*/
public static Date getLastNatureDayOnQuarter(Date date) {
Date lastDate = null;
Calendar calendar = null;
if (null != date) {
calendar = convertToCalendar(date);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
if (currentMonth >= 1 && currentMonth <= 3) {
calendar.set(Calendar.MONTH, 2);
calendar.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
calendar.set(Calendar.MONTH, 5);
calendar.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
calendar.set(Calendar.MONTH, 8);
calendar.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.DATE, 31);
}
lastDate = calendar.getTime();
}
return lastDate;
}
/**
* CL 20121101 根据指定日期获取该日期所在季度的第一个自然日
*
* @param date
* @return
*/
public static Date getFirstNatureDayOnQuarter(Date date) {
Date lastDate = null;
Calendar calendar = null;
if (null != date) {
calendar = convertToCalendar(date);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
if (currentMonth >= 1 && currentMonth <= 3) {
calendar.set(Calendar.MONTH, 0);
} else if (currentMonth >= 4 && currentMonth <= 6) {
calendar.set(Calendar.MONTH, 3);
} else if (currentMonth >= 7 && currentMonth <= 9) {
calendar.set(Calendar.MONTH, 6);
} else if (currentMonth >= 10 && currentMonth <= 12) {
calendar.set(Calendar.MONTH, 9);
}
calendar.set(Calendar.DATE, 1);
lastDate = calendar.getTime();
}
return lastDate;
}
/**
* fj 20130726 根据指定日期获取该日期所在季度最后一个月的第一个自然日
*
* @param date
* @return
*/
public static Date getFirstNatureDayOnQuarterEnd(Date date) {
Date lastDate = null;
Calendar calendar = null;
if (null != date) {
calendar = convertToCalendar(date);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
if (currentMonth >= 1 && currentMonth <= 3) {
calendar.set(Calendar.MONTH, 2);
} else if (currentMonth >= 4 && currentMonth <= 6) {
calendar.set(Calendar.MONTH, 5);
} else if (currentMonth >= 7 && currentMonth <= 9) {
calendar.set(Calendar.MONTH, 8);
} else if (currentMonth >= 10 && currentMonth <= 12) {
calendar.set(Calendar.MONTH, 11);
}
calendar.set(Calendar.DATE, 1);
lastDate = calendar.getTime();
}
return lastDate;
}
/**
* 格式化日期对象为yyyy-MM-dd格式,去掉时分秒等值,以免日期比较时出现问题
*
* @param date
* 参数日期
* @return 格式化后的日期对象
* @throws YssException
*/
public static Date standardDate(Date date) throws YssException {
if (date == null) {
return null;
}
try {
// fingbug异常,静态不安全 thp 20120613
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
date = dateFormat.parse(dateFormat.format(date));
} catch (Exception e) {
throw new YssException(e);
}
return date;
}
/**
* 获取本月最后一天
*
* @param format
* @return
*/
public static String getLastDayOfMonth(String format) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 1);
cal.add(Calendar.MONTH, 1);
cal.add(Calendar.DATE, -1);
return dateToString(cal.getTime(), format);
}
}