/**
* 日期工具类,如格式化日期、获得制定格式的日期、比较日期大小
*/
public class DateUtil {
int year;int month;
int day;
int hour;
int minute;
int second;
int millisecond;
String tempMonth;
GregorianCalendar gc;
Calendar calendar = new GregorianCalendar();
public DateUtil() {
}
public static Date parsetoDate(String dateStr,String format) throws ParseException{
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.parse(dateStr);
}
/**
* 格式化日期显示格式
*
* @param sdate :
* 原始日期
* @param format
* :格式化后日期格式
* @return 格式化后的日期显示
*/
public static String dateFormat(String sdate, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
java.sql.Date date = java.sql.Date.valueOf(sdate);
String dateString = formatter.format(date);
return dateString;
}
/**
* 获取两个日期间间所有日期字串数组 格式 yyyy-MM-dd 包括开始和截止日期
* @param startDay
* @param endDay
* @return
* @throws ParseException
*/
public static List<String> getDaysByDay(String startDay,String endDay) throws ParseException{
List<String> dateStrs = new ArrayList<String>();
Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = df.parse(startDay);
startCalendar.setTime(startDate);
Date endDate = df.parse(endDay);
endCalendar.setTime(endDate);
if(startDay.equals(endDay)){
dateStrs.add(startDay);
return dateStrs;
}
dateStrs.add(startDay);
while(true){
startCalendar.add(Calendar.DAY_OF_MONTH, 1);
if(startCalendar.getTimeInMillis() < endCalendar.getTimeInMillis()){//TODO 转数组或是集合,楼主看着写吧
dateStrs.add(df.format(startCalendar.getTime()));
}else{
break;
}
}
dateStrs.add(endDay);
return dateStrs;
}
/**
* 格式化日期显示格式
*
* @param date
* :原始日期
* @param format
* :格式化后日期格式
* @return 格式化后的日期显示
*/
public static String dateFormat(java.util.Date date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
String dateString = formatter.format(date);
return dateString;
}
/**
* 格式化Date类型日期为String类型并过滤指定的日期字符串
* @param date 原始日期
* @param format 格式化后日期格式
* @param filterDateStr 需要过滤的日期字符串,多个可用逗号隔开
* @return
*/
public static String dateFormatFilter(java.util.Date date, String format, String filterDateStr){
if(date == null)return "";
String dateStr = dateFormat(date, format);
if(StringUtilsCrew.isEmpty(dateStr) || filterDateStr.contains(dateStr))return "";
return dateStr;
}/**
* 求两个日期相差天数
*
* @param sd
* :起始日期,格式yyyy-MM-dd
* @param ed
* :终止日期,格式yyyy-MM-dd
* @return 两个日期相差天数
*/
public long getIntervalDays(String sd, String ed) {
return ((java.sql.Date.valueOf(ed)).getTime() - (java.sql.Date
.valueOf(sd)).getTime())
/ (3600 * 24 * 1000);
}
/**
* 求两个日期相差天数
*
* @param fDate
* :起始日期,格式yyyy-MM-dd
* @param oDate
* :终止日期,格式yyyy-MM-dd
* @return 两个日期相差天数,返回整数天数
*/
public static int getIntervalDaysInteger(Date fDate, Date oDate) {if (null == fDate || null == oDate) {
return -1;
}
long intervalMilli = oDate.getTime() - fDate.getTime();
return (int) (intervalMilli / (24 * 60 * 60 * 1000));
}
/**
* 格式化日期显示格式yyyy-MM-dd
*
* @param sdate
* :原始日期格式
* @return yyyy-MM-dd格式化后的日期显示
*/
public static String dateFormat(String sdate) {
return dateFormat(sdate, "yyyy-MM-dd");
}/**
* 取得当前日期和时间
*/
public void getDateCur() {
year = calendar.get(GregorianCalendar.YEAR);
month = (calendar.get(GregorianCalendar.MONTH) + 1);
day = calendar.get(GregorianCalendar.DAY_OF_MONTH);
gc = new GregorianCalendar(year, month, day);hour = calendar.get(GregorianCalendar.HOUR_OF_DAY);
minute = (calendar.get(GregorianCalendar.MINUTE));
second = (calendar.get(GregorianCalendar.SECOND));
}public String getDateTime(long datetime) {
java.util.Date date = new java.util.Date(datetime);java.util.Calendar tempCalendar = new java.util.GregorianCalendar();
tempCalendar.setTime(date);
return Integer.toString(tempCalendar.get(Calendar.YEAR)) + "-"
+ Integer.toString(tempCalendar.get(Calendar.MONTH) + 1) + "-"
+ Integer.toString(tempCalendar.get(Calendar.DAY_OF_MONTH))
+ " "
+ Integer.toString(tempCalendar.get(Calendar.HOUR_OF_DAY))
+ ":" + Integer.toString(tempCalendar.get(Calendar.MINUTE))
+ ":"
+ Integer.toString(tempCalendar.get(GregorianCalendar.SECOND));
}/**
* 获得当前时间的小时数
*
* @return 当前时间的小时数
*/
public String getTimeHour() {
getDateCur();
return Integer.toString(this.hour);
}/**
* 获得当前时间的分钟数
*
* @return 当前时间的分钟数
*/
public String getTimeMinute() {
getDateCur();
return Integer.toString(this.minute);
}/**
* 得到当前时间
*
* @return 当前时间,格式yyyy-MM-dd hh:mm:ss
*/
public String getDateTime() {
getDateCur();
return Integer.toString(this.year) + "-" + Integer.toString(this.month)
+ "-" + Integer.toString(this.day) + " "
+ Integer.toString(this.hour) + ":"
+ Integer.toString(this.minute) + ":"
+ Integer.toString(this.second);
}/**
* 获得固定间隔的日期,如前60天dateAdd(-60),后60天dateAdd(60)
*
* @param amount
* :距今天的间隔日期长度,向前为负,向后为正
* @return 固定间隔的日期,格式yyyy-MM-dd
*/
public String dateAdd(int amount) {
Calendar cal = new GregorianCalendar();
java.util.Date trialTime = new java.util.Date();
cal.setTime(trialTime);
cal.add(GregorianCalendar.DATE, amount);
int y = cal.get(GregorianCalendar.YEAR);
int m = cal.get(GregorianCalendar.MONTH) + 1;
int d = cal.get(GregorianCalendar.DAY_OF_MONTH);
String mm = "";
if(m<10){
mm = "0"+m;
}else{
mm = ""+m;
}
String dd = "";
if(d<10){
dd = "0"+d;
}else{
dd = ""+d;
}
String ymd = Integer.toString(y) + "-" + mm + "-"
+ dd;
return dateFormat(ymd, "yyyy-MM-dd");
}/**
* 获得固定间隔的日期,如前60天dateAdd(-60),后60天dateAdd(60)
*
* @param sourceDate
* :某一日期
* @param amount
* :距某一日期的间隔日期长度,向前为负,向后为正
* @return 固定间隔的日期,格式yyyy-MM-dd
*/
public String dateAdd(String sourceDate, int amount) {
Calendar cal = new GregorianCalendar();
java.sql.Date trialTime = java.sql.Date.valueOf(sourceDate);
cal.setTime(trialTime);
cal.add(GregorianCalendar.DATE, amount);
int y = cal.get(GregorianCalendar.YEAR);
int m = cal.get(GregorianCalendar.MONTH) + 1;
int d = cal.get(GregorianCalendar.DAY_OF_MONTH);
String mm = "";
if(m<10){
mm = "0"+m;
}else{
mm = ""+m;
}
String dd = "";
if(d<10){
dd = "0"+d;
}else{
dd = ""+d;
}
String ymd = Integer.toString(y) + "-" + mm + "-"
+ dd;return dateFormat(ymd, "yyyy-MM-dd");
}/**
* 得到每月的最后日期
*
* @param theMonth
* :月份
* @return 该月的最后日期
*/
public int daysInMonth(int theMonth) {
int[] daysInMonths = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
daysInMonths[1] += gc.isLeapYear(year) ? 1 : 0;
return daysInMonths[theMonth - 1];
}/**
* 输入yyyy/mm字符串,得到该月的最大日期
*
* @param yMonth
* :某年的某一月份
* @return 该年该月的最大日期
*/
public int lastDayInMonth(String yMonth) {
String thisDate = yMonth;
// thisDate = dateFormat(thisDate,"yyyy-MM");
month = Integer.parseInt(thisDate.substring(5, 7));
year = Integer.parseInt(thisDate.substring(0, 4));
gc = new GregorianCalendar(year, month, 1);
int lastday = daysInMonth(month);
return lastday;
}/**
* 得到上一个月的月份
*
* @return 上一个月的月份
*/
public int reduceMonth() {
if (month == 1) {
month = 12;
year--;
} else {
month--;
}
return month;
}/**
* 得到下一个月的月份
*
* @return 下一个月的月份
*/
public int nextMonth() {
if (month == 12) {
month = 1;
year++;
} else {
month++;
}
return month;
}/**
* 已知年月yyyy-MM,获得下月yyyy-MM
*
* @param thisMonth
* :当前年月
* @return 下一年月
*/
public String getNextMonth(String thisMonth) {
int intYear = Integer.parseInt(thisMonth.substring(0, 4));
int intMonth = Integer.parseInt(thisMonth.substring(thisMonth
.indexOf("-") + 1));
if (intMonth == 12) {
intMonth = 1;
intYear++;
} else {
intMonth++;
}
if (intMonth < 10) {
tempMonth = "0" + Integer.toString(intMonth);
} else {
tempMonth = Integer.toString(intMonth);
}
return intYear + "-" + tempMonth;
}/**
* 获得起始年月yyyy-MM与终止月yyyy-MM之间跨度的月数
*
* @param beginMonth
* :起始年月yyyy-MM
* @param endMonth
* :终止月yyyy-MM
* @return 月份之间的跨度
*/
public static int getInterval(String beginMonth, String endMonth) {
int intBeginYear = Integer.parseInt(beginMonth.substring(0, 4));
int intBeginMonth = Integer.parseInt(beginMonth.substring(beginMonth
.indexOf("-") + 1));
int intEndYear = Integer.parseInt(endMonth.substring(0, 4));
int intEndMonth = Integer.parseInt(endMonth.substring(endMonth
.indexOf("-") + 1));return (intEndYear - intBeginYear) * 12 + (intEndMonth - intBeginMonth)
+ 1;
}
/**
* 功能:计算两日期之间的天数
* @param String startDay 开始日期
* @param String endDay 结束日期
* @return int 天数
*/
public static int getDays(String startDay, String endDay) {
Calendar sDate = Calendar.getInstance();
Calendar eDate = Calendar.getInstance();
String tmpDay = "";int year;
int month;
int day;tmpDay = formatDateYYYYMMDD(startDay);
year = Integer.parseInt(tmpDay.substring(0, 4));
month = Integer.parseInt(tmpDay.substring(5, 7));
day = Integer.parseInt(tmpDay.substring(8, 10));
month -= 1;
sDate.set(year, month, day, 0, 0, 0);tmpDay = formatDateYYYYMMDD(endDay);
year = Integer.parseInt(tmpDay.substring(0, 4));
month = Integer.parseInt(tmpDay.substring(5, 7));
day = Integer.parseInt(tmpDay.substring(8, 10));
month -= 1;
eDate.set(year, month, day, 0, 0, 0);day =
(int) ((eDate.getTime().getTime() - sDate.getTime().getTime())
/ 1000
/ 3600
/ 24);
return day;
}
/**
* 功能:计算两日期之间的工作日天数
* @param startDay 开始日期,格式为YYYY-MM-DD
* @param endDay 结束日期,格式为YYYY-MM-DD
* @return 工作日天数
* @throws SQLException
* @throws DataAccessException
*/
public static int getWorkDays(Date startDay, Date endDay) throws DataAccessException, SQLException{
if(startDay==null || endDay==null){
return 0;
}
int days = 0;
JDBCDataSource datasource = JDBCDataSource.getJDBCDataSource();
PreparedStatement ps = null;
ResultSet rs = null;
Calendar sCal = Calendar.getInstance();
Calendar eCal = Calendar.getInstance();
sCal.setTime(startDay);
eCal.setTime(endDay);
if(eCal.compareTo(sCal)==0){//如果开始日期与结束日期一样,直接返回1天
days = 1;
return days;
} else{//当开始日期与结束日期不一样时
while(eCal.compareTo(sCal)>0){
//先算出这个时间段内有多少工作日(只去除周末,不去除节假日)
int day = sCal.get(Calendar.DAY_OF_WEEK);
if(day!=Calendar.SATURDAY&&day!=Calendar.SUNDAY){
days++;
}
sCal.add(Calendar.DATE, 1);
}
}
//再到节假日表中查询在这个时间段内有没有维护的节假日
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startDayStr = df.format(startDay).substring(0,10)+" 00:00:00";
String endDayStr = df.format(endDay);
String sql = "SELECT * FROM T_Dict_Holiday WHERE holiday_date BETWEEN to_date('"+startDayStr+"','yyyy-mm-dd hh24:mi:ss') AND to_date('"+endDayStr+"','yyyy-mm-dd hh24:mi:ss') and valid_flag = '1'";
int jjr = 0;
int gzr = 0;
ps = datasource.getConnection().prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Date holiday_date = rs.getDate("holiday_date");
if("1".equals(rs.getString("holiday_flag"))){// 节假日
// 如果这天本来就是周末,又维护的是节假日,就不应该再加一天了,上面的days计算的就是去掉周末的
boolean flag = isWeekend(holiday_date);
if(!flag){
jjr++;
}
} else if("0".equals(rs.getString("holiday_flag"))){// 工作日
// 如果这天是周末,又维护为工作日,才加一天,如果本来就是工作日,在上面已经计算进去了,不需要再加
boolean flag = isWeekend(holiday_date);
if(flag){
gzr++;
}
}
}
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
days = days-jjr+gzr;
return days;
}
/**
* 是否为周末
* @param date
* @return
*/
public static boolean isWeekend(Date date){
boolean flag = false;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_WEEK);
if(day==Calendar.SATURDAY||day==Calendar.SUNDAY){
flag = true;
}
return flag;
}
/**
* 功能:将日期转为标准格式YYYY-MM-DD
* @param String dt (YYYY-M-D,YYYY-M-DD,YYYY-MM-D)
*/
public static String formatDateYYYYMMDD(String dt) {
String retDt = "";
try {
if (null == dt || dt.length() == 0)
return "";
else {
String year = "";
String month = "";
String date = "";
int idx = dt.indexOf("-", 5);
if (idx == -1)
return "";
year = dt.substring(0, 4);
month = dt.substring(5, idx);
if (month.length() == 1)
month = "0" + month;
date = dt.substring(idx + 1);
if (date.length() == 1)
date = "0" + date;
retDt = year + "-" + month + "-" + date;
}
return retDt;
} catch (Exception e) {
return "";
}
}
/**
* 得到前一天的日期
*
* @return 前一天的日期
*/
public int reduceDay() {
if (day == 1) {
month = reduceMonth();
day = daysInMonth(month);
} else {
day--;
}
return day;
}/**
* 得到昨天的日期,返回格式如yyyy-mm-dd
*
* @return java.lang.String
*/
public String getYestoday() {
getDateCur();
day = reduceDay();
String mon = "";
String day1 = "";
if (day < 10) {
day1 = "0" + Integer.toString(day);
} else {
day1 = Integer.toString(day);
}
if (month < 10) {
mon = "0" + Integer.toString(month);
} else {
mon = Integer.toString(month);
}
return Integer.toString(year) + "-" + mon + "-" + day1;
}/**
* 得到上月 返回格式如yyyy-mm
*
* @return java.lang.String
*/
public String getLastMonth() {
getDateCur();
month = reduceMonth();
if (month < 10) {
tempMonth = "0" + Integer.toString(month);
} else {
tempMonth = Integer.toString(month);
}
return Integer.toString(year) + "-" + tempMonth;
}/**
* 取得与当前时间相隔几年后的时间
*
* @param interval
* :年份间隔
* @return 与当前时间相隔几年后的时间,返回格式yyyy-MM-dd
*/
public String getDateAfterYear(int interval) {
this.getToday();year = year + interval;
String mon = "";
String day1 = "";day = this.reduceDay();
if (day > this.daysInMonth(month)) {
day = this.daysInMonth(month);
}if (day < 10) {
day1 = "0" + Integer.toString(day);
} else {
day1 = Integer.toString(day);
;
}
if (month < 10) {
mon = "0" + Integer.toString(month);
} else {
mon = Integer.toString(month);
}
return Integer.toString(year) + "-" + mon + "-" + day1;
}/**
* 取得与当前时间相隔几个月后的时间
*
* @param interval
* :月份间隔
* @return 与当前时间相隔几个月后的时间,返回格式yyyy-MM-dd
*/
public String getDateAfterMonth(int interval) {
this.getToday();if (month + interval > 0) {
if (month + interval > 12) {
// 求余
int yearIncrease = (month + interval) / 12;year = year + yearIncrease;
// 求倍数
month = (month + interval) % 12;
} else {
month = month + interval;
}
} else {
if (Math.abs(month + interval) > 12) {
// 求余
int yearIncrease = Math.abs(month + interval) / 12;year = year - yearIncrease;
// 求倍数
month = 12 - Math.abs(month + interval) % 12;
} else {
year = year - 1;
month = 12 - Math.abs(month + interval);
}
}String mon = "";
String day1 = "";day = this.reduceDay();
if (day > this.daysInMonth(month)) {
day = this.daysInMonth(month);
}if (day < 10) {
day1 = "0" + Integer.toString(day);
} else {
day1 = Integer.toString(day);
;
}
if (month < 10) {
mon = "0" + Integer.toString(month);
} else {
mon = Integer.toString(month);
}
return Integer.toString(year) + "-" + mon + "-" + day1;
}/**
* 得到去年 返回格式如yyyy
*
* @return java.lang.String
*/
public String getLastYear() {
getDateCur();
year--;
return Integer.toString(year);
}/**
* 得到今天的日期,返回格式如yyyy-mm-dd
*
* @return java.lang.String
*/
public String getToday() {
getDateCur();
String mon = "";
String day1 = "";
if (day < 10) {
day1 = "0" + Integer.toString(day);
} else {
day1 = Integer.toString(day);
;
}
if (month < 10) {
mon = "0" + Integer.toString(month);
} else {
mon = Integer.toString(month);
}
return Integer.toString(year) + "-" + mon + "-" + day1;
}/**
* 获得本月,返回格式如yyyy-mm
*
* @return java.lang.String
*/
public String getCurMonth() {
getDateCur();
if (month < 10) {
tempMonth = "0" + Integer.toString(month);
} else {
tempMonth = Integer.toString(month);
}
return Integer.toString(year) + "-" + tempMonth;
}/**
* 获得今年,返回格式如yyyy
*
* @return java.lang.String
*/
public String getCurYear() {
getDateCur();
return Integer.toString(year);
}/**
* 返回上月的最大日期 格式为yyyy-mm-dd
*
* @return java.lang.String
*/
public String getPriorMonthLastDay() {
getDateCur();
month = reduceMonth();
day = daysInMonth(month);
return dateFormat(Integer.toString(year) + "-"
+ Integer.toString(month) + "-" + Integer.toString(day),
"yyyy-MM-dd");
}/**
* 返回上月的最小日期 格式为yyyy-mm-dd
*
* @return java.lang.String
*/
public String getPriorMonthFirstDay() {
getDateCur();
month = reduceMonth();
return dateFormat(Integer.toString(year) + "-"
+ Integer.toString(month) + "-01", "yyyy-MM-dd");
}/**
* 返回本月的最大日期 格式为yyyy-mm-dd
*
* @return java.lang.String
*/
public String getThisMonthLastDay() {
getDateCur();
day = daysInMonth(month);
return dateFormat(Integer.toString(year) + "-"
+ Integer.toString(month) + "-" + Integer.toString(day),
"yyyy-MM-dd");
}/**
* 返回本月的最小日期 格式为yyyy-mm-dd
*
* @return java.lang.String
*/
public String getThisMonthFirstDay() {
getDateCur();
return dateFormat(Integer.toString(year) + "-"
+ Integer.toString(month) + "-01", "yyyy-MM-dd");
}/**
* 返回下月的最大日期 格式为yyyy-mm-dd
*
* @return java.lang.String
*/
public String getNextMonthLastDay() {
getDateCur();
month = nextMonth();
day = daysInMonth(month);
return dateFormat(Integer.toString(year) + "-"
+ Integer.toString(month) + "-" + Integer.toString(day),
"yyyy-MM-dd");
}/**
* 返回下月的最小日期 格式为yyyy-mm-dd
*
* @return java.lang.String
*/
public String getNextMonthFirstDay() {
getDateCur();
month = nextMonth();
return dateFormat(Integer.toString(year) + "-"
+ Integer.toString(month) + "-01", "yyyy-MM-dd");
}/**
* 返回日期 格式为yyyymmddhhmmssfff
*
* @return java.lang.String
*/
public String getTimeCur() {
year = calendar.get(GregorianCalendar.YEAR);
month = (calendar.get(GregorianCalendar.MONTH) + 1);
day = calendar.get(GregorianCalendar.DAY_OF_MONTH);
hour = calendar.get(GregorianCalendar.HOUR_OF_DAY);
minute = (calendar.get(GregorianCalendar.MINUTE));
second = (calendar.get(GregorianCalendar.SECOND));
millisecond = (calendar.get(GregorianCalendar.MILLISECOND));
String months = Integer.toString(month);
String days = Integer.toString(day);
String hours = Integer.toString(hour);
String minutes = Integer.toString(minute);
String seconds = Integer.toString(second);
String milliseconds = Integer.toString(millisecond);
if (month < 10) {
months = "0" + months;
}
if (day < 10) {
days = "0" + days;
}
if (hour < 10) {
hours = "0" + hours;
}
if (minute < 10) {
minutes = "0" + minutes;
}
if (second < 10) {
seconds = "0" + seconds;
}
if (millisecond < 10) {
milliseconds = "00" + milliseconds;
} else if (millisecond < 100) {
milliseconds = "0" + milliseconds;
}
return Integer.toString(year) + months + days + hours + minutes
+ seconds + milliseconds;
}/**
* 格式化日期值,对一位的月日时分妙补零
*
* @param str
* :String 日期字符串
* @return String 格式化后的日期字符串
*/
public String formatDate(String str) {
String yy = "", mm = "", dd = "", hh = "", ff = "", mmm = "";
int m = 0;
int j = 0;
for (int i = 0; i <= str.length(); i++) {
if (i == str.length() || str.substring(i, i + 1).equals("-")
|| str.substring(i, i + 1).equals(" ")
|| str.substring(i, i + 1).equals(":")) {
m++;
if (m == 1) {
yy = str.substring(j, i);
}
if (m == 2) {
mm = str.substring(j, i);
if (mm.length() == 1) {
mm = "0" + mm;
}
}
if (m == 3) {
dd = str.substring(j, i);
if (dd.length() == 1) {
dd = "0" + dd;
}
}
if (m == 4) {
hh = str.substring(j, i);
if (hh.length() == 1) {
hh = "0" + hh;
}
}
if (m == 5) {
ff = str.substring(j, i);
if (ff.length() == 1) {
ff = "0" + ff;
}
}
if (m == 6) {
mmm = str.substring(j, i);
if (mmm.length() == 1) {
mmm = "0" + mmm;
}
}
j = i + 1;
}
}
return (yy + "-" + mm + "-" + dd + " " + hh + ":" + ff + ":" + mmm);}
/**
* 返回中文年月日格式,不带零(2017年1月1日)
* @param str
* @return
*/
public static String formatDateCnCert(String str) {
String yy = "", mm = "", dd = "", hh = "", ff = "", mmm = "";
int m = 0;
int j = 0;
for (int i = 0; i <= str.length(); i++) {
if (i == str.length() || str.substring(i, i + 1).equals("-")
|| str.substring(i, i + 1).equals(" ")
|| str.substring(i, i + 1).equals(":")) {
m++;
if (m == 1) {
yy = str.substring(j, i);
}
if (m == 2) {
mm = str.substring(j, i);
if (mm.length() == 2 && mm.charAt(0)=='0') {
mm = mm.substring(1,2);
}
}
if (m == 3) {
dd = str.substring(j, i);
if (dd.length() == 2 && dd.charAt(0)=='0') {
dd = dd.substring(1,2);
}
}
if (m == 4) {
hh = str.substring(j, i);
if (hh.length() == 1) {
hh = "0" + hh;
}
}
if (m == 5) {
ff = str.substring(j, i);
if (ff.length() == 1) {
ff = "0" + ff;
}
}
if (m == 6) {
mmm = str.substring(j, i);
if (mmm.length() == 1) {
mmm = "0" + mmm;
}
}
j = i + 1;
}
}
return yy+"年"+mm+"月"+dd+"日";
}
/**
* 返回英文格式日期static:14 Jan, 2008
* @param str
* @return
*/
public static String formatDateEnStatic(String str) {
String yy = "", mm = "", dd = "", hh = "", ff = "", mmm = "";
int m = 0;
int j = 0;
for (int i = 0; i <= str.length(); i++) {
if (i == str.length() || str.substring(i, i + 1).equals("-")
|| str.substring(i, i + 1).equals(" ")
|| str.substring(i, i + 1).equals(":")) {
m++;
if (m == 1) {
yy = str.substring(j, i);
}
if (m == 2) {
mm = str.substring(j, i);
/*if (mm.length() == 1) {
mm = "0" + mm;
}*/
}
if (m == 3) {
dd = str.substring(j, i);
/*if (dd.length() == 2) {
dd = dd.substring(1,2);
} */
if(Integer.parseInt(dd) < 10){
if (dd.length() == 2) {
dd = dd.substring(1,2);
}
}else{
if (dd.length() == 2) {
dd = dd.substring(0,2);
}
}
}
if (m == 4) {
hh = str.substring(j, i);
if (hh.length() == 1) {
hh = "0" + hh;
}
}
if (m == 5) {
ff = str.substring(j, i);
if (ff.length() == 1) {
ff = "0" + ff;
}
}
if (m == 6) {
mmm = str.substring(j, i);
if (mmm.length() == 1) {
mmm = "0" + mmm;
}
}
j = i + 1;
}
}
if ("01".equals(mm)) {
mm = "Jan";
} else if ("02".equals(mm)) {
mm = "Feb";
} else if ("03".equals(mm)) {
mm = "Mar";
} else if ("04".equals(mm)) {
mm = "Apr";
} else if ("05".equals(mm)) {
mm = "May";
} else if ("06".equals(mm)) {
mm = "June";
} else if ("07".equals(mm)) {
mm = "July";
} else if ("08".equals(mm)) {
mm = "Aug";
} else if ("09".equals(mm)) {
mm = "Sept";
} else if ("10".equals(mm)) {
mm = "Oct";
} else if ("11".equals(mm)) {
mm = "Nov";
} else if ("12".equals(mm)) {
mm = "Dec";
}
return dd+" "+mm+"."+yy;
}
/**
* 返回英文格式日期static:
* 如: Jan 14, 2008
* @param str
* @return
*/
public static String formatDateEn2(String str) {
String yy = "", mm = "", dd = "", hh = "", ff = "", mmm = "";
int m = 0;
int j = 0;
for (int i = 0; i <= str.length(); i++) {
if (i == str.length() || str.substring(i, i + 1).equals("-")
|| str.substring(i, i + 1).equals(" ")
|| str.substring(i, i + 1).equals(":")) {
m++;
if (m == 1) {
yy = str.substring(j, i);
}
if (m == 2) {
mm = str.substring(j, i);
/*if (mm.length() == 1) {
mm = "0" + mm;
}*/
}
if (m == 3) {
dd = str.substring(j, i);
/*if (dd.length() == 2) {
dd = dd.substring(1,2);
}*/
if(Integer.parseInt(dd) < 10){
if (dd.length() == 2) {
dd = dd.substring(1,2);
}
}else{
if (dd.length() == 2) {
dd = dd.substring(0,2);
}
}
}
if (m == 4) {
hh = str.substring(j, i);
if (hh.length() == 1) {
hh = "0" + hh;
}
}
if (m == 5) {
ff = str.substring(j, i);
if (ff.length() == 1) {
ff = "0" + ff;
}
}
if (m == 6) {
mmm = str.substring(j, i);
if (mmm.length() == 1) {
mmm = "0" + mmm;
}
}
j = i + 1;
}
}
if ("01".equals(mm)) {
mm = "Jan";
} else if ("02".equals(mm)) {
mm = "Feb";
} else if ("03".equals(mm)) {
mm = "Mar";
} else if ("04".equals(mm)) {
mm = "Apr";
} else if ("05".equals(mm)) {
mm = "May";
} else if ("06".equals(mm)) {
mm = "June";
} else if ("07".equals(mm)) {
mm = "July";
} else if ("08".equals(mm)) {
mm = "Aug";
} else if ("09".equals(mm)) {
mm = "Sept";
} else if ("10".equals(mm)) {
mm = "Oct";
} else if ("11".equals(mm)) {
mm = "Nov";
} else if ("12".equals(mm)) {
mm = "Dec";
}
return mm+" "+dd+","+yy;
}
/**
* 返回英文格式日期:October 14, 2008
* @param str
* @return
*/
public String formatDateEn(String str) {
String yy = "", mm = "", dd = "", hh = "", ff = "", mmm = "";
int m = 0;
int j = 0;
for (int i = 0; i <= str.length(); i++) {
if (i == str.length() || str.substring(i, i + 1).equals("-")
|| str.substring(i, i + 1).equals(" ")
|| str.substring(i, i + 1).equals(":")) {
m++;
if (m == 1) {
yy = str.substring(j, i);
}
if (m == 2) {
mm = str.substring(j, i);
if (mm.length() == 1) {
mm = "0" + mm;
}
}
if (m == 3) {
dd = str.substring(j, i);
// if (dd.length() == 1) {
// dd = "0" + dd;
// }
}
if (m == 4) {
hh = str.substring(j, i);
if (hh.length() == 1) {
hh = "0" + hh;
}
}
if (m == 5) {
ff = str.substring(j, i);
if (ff.length() == 1) {
ff = "0" + ff;
}
}
if (m == 6) {
mmm = str.substring(j, i);
if (mmm.length() == 1) {
mmm = "0" + mmm;
}
}
j = i + 1;
}
}
if ("01".equals(mm)) {
mm = "January";
} else if ("02".equals(mm)) {
mm = "February";
} else if ("03".equals(mm)) {
mm = "March";
} else if ("04".equals(mm)) {
mm = "April";
} else if ("05".equals(mm)) {
mm = "May";
} else if ("06".equals(mm)) {
mm = "June";
} else if ("07".equals(mm)) {
mm = "July";
} else if ("08".equals(mm)) {
mm = "August";
} else if ("09".equals(mm)) {
mm = "September";
} else if ("10".equals(mm)) {
mm = "October";
} else if ("11".equals(mm)) {
mm = "November";
} else if ("12".equals(mm)) {
mm = "December";
}
return mm+" "+dd+","+yy;
}
/**
* 返回中文年月日格式(带零,2017年01月01日)
* @param str
* @return
*/
public static String formatDateCn(String str) {
String yy = "", mm = "", dd = "", hh = "", ff = "", mmm = "";
int m = 0;
int j = 0;
for (int i = 0; i <= str.length(); i++) {
if (i == str.length() || str.substring(i, i + 1).equals("-")
|| str.substring(i, i + 1).equals(" ")
|| str.substring(i, i + 1).equals(":")) {
m++;
if (m == 1) {
yy = str.substring(j, i);
}
if (m == 2) {
mm = str.substring(j, i);
if (mm.length() == 1) {
mm = "0" + mm;
}
}
if (m == 3) {
dd = str.substring(j, i);
if (dd.length() == 1) {
dd = "0" + dd;
}
}
if (m == 4) {
hh = str.substring(j, i);
if (hh.length() == 1) {
hh = "0" + hh;
}
}
if (m == 5) {
ff = str.substring(j, i);
if (ff.length() == 1) {
ff = "0" + ff;
}
}
if (m == 6) {
mmm = str.substring(j, i);
if (mmm.length() == 1) {
mmm = "0" + mmm;
}
}
j = i + 1;
}
}
return yy+"年"+mm+"月"+dd+"日";
}
/**
* 判断年份是否为闰年 ------------------------------------------------- 润年的条件:
* 1)能被4整除,但不能被100整除的年份; 2)能被100整除,又能被400整除的年份;
* ------------------------------------------------- 设y为被检测的年份,则算法可表示如下:
* S2:若y不能被4整除,则输出n“不是闰年” S3:若y能被4整除,不能被100整除,则输出y“是闰年”
* S4:若y能被100整除,又能被400整除,输出y“是闰年” S5:输出y“不是闰年”
*
* @param y
* @return
*/
public static boolean ifLeapYear(int y) {
boolean bFlag = false;
if ((y % 4) != 0) {
bFlag = false;
} else if ((y % 100) != 0) {
bFlag = true;
} else if ((y % 400) == 0) {
bFlag = true;
} else {
bFlag = false;
}return bFlag;
}/**
* 根据出生日期计算年龄,只取年龄的整数,如65岁364天也是65周岁,如果需要大于65时如何如何,可能会导致65岁1天到65岁364天不符合这个条件
*
* @param birthDay
* @return
*/
public static int getAge(Date birthDay) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String birthDayStr = sdf.format(birthDay);
String nowDayStr = sdf.format(new Date());int year = Integer.parseInt(birthDayStr.substring(0, 4));
int day = Integer.parseInt(birthDayStr.substring(4, 8));int nowYear = Integer.parseInt(nowDayStr.substring(0, 4));
int nowDay = Integer.parseInt(nowDayStr.substring(4, 8));int age = nowYear - year;
if (day > nowDay) {
age = age - 1;
}return age;
}
/**
* 获取真实年龄,精确到小数
* @param birthDay
* @param endDate
* @return
* @throws ParseException
*/
public double getRealAge(Date birthDay, Date endDate) throws ParseException {
double age = 0.0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String birthDayStr = sdf.format(birthDay);
String nowDayStr = sdf.format(endDate);int year = Integer.parseInt(birthDayStr.substring(0, 4));
int day = Integer.parseInt(birthDayStr.substring(4, 8));int nowYear = Integer.parseInt(nowDayStr.substring(0, 4));
int nowDay = Integer.parseInt(nowDayStr.substring(4, 8));int ageInt = nowYear - year;
Date startDate = null;
if (day > nowDay) { //2000-12-31 2019-1-1
ageInt = ageInt - 1;
startDate = sdf.parse((nowYear-1)+""+(day<1000?("0"+day):day));
}else{//2000-1-1 2019-12-31
startDate = sdf.parse((nowYear)+""+(day<1000?("0"+day):day));
}
int betweenDays = DateUtilsCrew.daysBetween(startDate, endDate);
double ageDouble = 0.0;
if(DateUtilsCrew.ifLeapYear(nowYear)){
ageDouble = betweenDays/366.0;
}else{
ageDouble = betweenDays/365.0;
}
age = ageInt+ageDouble;return age;
}/**
* 根据当前日期,取得当前日期之前之后n个月的日期
*
* @param ksrq
* @param yf
* @return
*/
public static Date getJzrq(Date ksrq, int yf) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String sKsrq = sdf.format(ksrq);int ksYear = Integer.parseInt(sKsrq.substring(0, 4));
int ksMonth = Integer.parseInt(sKsrq.substring(4, 6));
int ksDay = Integer.parseInt(sKsrq.substring(6, 8));ksMonth = ksMonth + yf;
if (ksMonth > 12) {
ksYear = ksYear + (ksMonth / 12);
ksMonth = ksMonth % 12;
if (ksMonth == 0) {
ksYear = ksYear - 1;
ksMonth = 12;
}
}switch (ksMonth) {
case 4:
if (ksDay > 30) {
ksDay = 30;
}
break;
case 6:
if (ksDay > 30) {
ksDay = 30;
}
break;
case 9:
if (ksDay > 30) {
ksDay = 30;
}
break;
case 11:
if (ksDay > 30) {
ksDay = 30;
}
break;case 2:
if (ksDay > 28) {
if (DateUtil.ifLeapYear(ksYear)) {
ksDay = 29;
} else {
ksDay = 28;
}
}
break;}
StringBuffer sbDate = new StringBuffer();
sbDate.append(ksYear);
if (ksMonth < 10) {
sbDate.append("0");
}
sbDate.append(ksMonth);
if (ksDay < 10) {
sbDate.append("0");
}
sbDate.append(ksDay);Date jzrq = sdf.parse(sbDate.toString());
return jzrq;}
/**
* 根据当前日期,取得当前日期之前n个月的日期
*
* @param ksrq
* @param yf
* @return
*/
public static Date getKsrq(Date dqrq, int yf) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String sDqrq = sdf.format(dqrq);int dqYear = Integer.parseInt(sDqrq.substring(0, 4));
int dqMonth = Integer.parseInt(sDqrq.substring(4, 6));
int dqDay = Integer.parseInt(sDqrq.substring(6, 8));dqMonth = dqMonth - yf;
if (dqMonth < 0) {
dqYear = dqYear - (-dqMonth / 12 + 1);
dqMonth = 12 + dqMonth % 12;
if (dqMonth == 0) {
dqYear = dqYear - 1;
dqMonth = 12;
}
}switch (dqMonth) {
case 4:
if (dqDay > 30) {
dqDay = 30;
}
break;
case 6:
if (dqDay > 30) {
dqDay = 30;
}
break;
case 9:
if (dqDay > 30) {
dqDay = 30;
}
break;
case 11:
if (dqDay > 30) {
dqDay = 30;
}
break;case 2:
if (dqDay > 28) {
if (DateUtil.ifLeapYear(dqYear)) {
dqDay = 29;
} else {
dqDay = 28;
}
}
break;}
StringBuffer sbDate = new StringBuffer();
sbDate.append(dqYear);
if (dqMonth < 10) {
sbDate.append("0");
}
sbDate.append(dqMonth);
if (dqDay < 10) {
sbDate.append("0");
}
sbDate.append(dqDay);Date jzrq = sdf.parse(sbDate.toString());
return jzrq;}
/**
* 比较两个日期
*
* @param startDate
* @param endDate
* @return
* @throws ParseException
*/
public static String compareDate(String startDate, String endDate)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
long start = formatter.parse(startDate).getTime();
long end = formatter.parse(endDate).getTime();
if (end > start) {
return "more";
} else if (end < start) {
return "less";
} else {
return "equal";
}
}public static String ConvertNumberToChinese(String x) {
// 数字 数组
String[] Nums = new String[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒",
"捌", "玖" };
// 位 数组
String[] Digits = new String[] { "", "拾", "佰", "仟" };
// 单位 数组
String[] Units = new String[] { "", "[万]", "[亿]", "[万亿]" };String S = ""; // 返回值
int p = 0; // 字符位置指针
int m = x.length() % 4; // 取模// 四位一组得到组数
int k = (m > 0 ? x.length() / 4 + 1 : x.length() / 4);// 外层循环在所有组中循环
// 从左到右 高位到低位 四位一组 逐组处理
// 每组最后加上一个单位: "[万亿]","[亿]","[万]"
for (int i = k; i > 0; i--) {
int L = 4;
if (i == k && m != 0) {
L = m;
}
// 得到一组四位数 最高位组有可能不足四位
String s = x.substring(p, p + L);
int l = s.length();// 内层循环在该组中的每一位数上循环 从左到右 高位到低位
for (int j = 0; j < l; j++) {
// 处理改组中的每一位数加上所在位: "仟","佰","拾",""(个)
int n = java.lang.Integer.parseInt(s.substring(j, j + 1));
if (n == 0) {
if ((j < l - 1)
&& (java.lang.Integer.parseInt(s.substring(j + 1,
j + 1 + 1)) > 0) // 后一位(右低)
&& !S.endsWith(Nums[n])) {
S += Nums[n];
}
} else {
// 处理 1013 一千零"十三", 1113 一千一百"一十三"
if (!(n == 1 && (S.endsWith(Nums[0]) || S.length() == 0) && j == l - 2)) {
S += Nums[n];
}
S += Digits[l - j - 1];
}
}
p += L;
// 每组最后加上一个单位: [万],[亿] 等
if (i < k) // 不是最高位的一组
{
if (java.lang.Integer.parseInt(s) != 0) {
// 如果所有 4 位不全是 0 则加上单位 [万],[亿] 等
S += Units[i - 1];
}
} else {
// 处理最高位的一组,最后必须加上单位
S += Units[i - 1];
}
}
return S;
}
/**
* 将日期转换为中文大写 date 格式 06(日)-01(月)-2011(年)
* @param date
* @return
*/
public static String dateToCnDate(String date) {
String result = "";
String[] cnDate = new String[]{"〇","一","二","三","四","五","六","七","八","九"};
String ten = "十";
String[] dateStr = date.split("-");
for (int i=0; i<dateStr.length; i++) {
for (int j=0; j<dateStr[i].length(); j++) {
String charStr = dateStr[i];
String str = String.valueOf(charStr.charAt(j));
if (charStr.length() == 2) {
if (charStr.equals("10")) {
result += ten;
break;
} else {
if (j == 0) {
if (charStr.charAt(j) == '1')
result += ten;
else if (charStr.charAt(j) == '0')
result += "";
else
result += cnDate[Integer.parseInt(str)] + ten;
}
if (j == 1) {
if (charStr.charAt(j) == '0')
result += "";
else
result += cnDate[Integer.parseInt(str)];
}
}
} else {
result += cnDate[Integer.parseInt(str)];
}
}
if (i == 0) {
result += " "; //日
continue;
}
if (i == 1) {
result += " ";//月
continue;
}
if (i == 2) {
result += " ";//年
continue;
}
}
return result;
}
/**
* 获取某年某月最后一天(包括闰年)
* @param yyyy
* @param mm
* @return
*/
public static int getLastDay(int yyyy,int mm){
//计算每个月的最后一天
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, yyyy);
c.set(Calendar.MONTH, mm-1);
return c.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* 计算两个日期之间历时,返回如:1天3小时34分21秒
* @param beginTime
* @param endTime
* @return
*/
public static String getTimesFromTime(String beginTime, String endTime) {
String result = "";
int days = 0;
int hours = 0;
int minute = 0;
int second = 0;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if(StringUtilsCrew.isNotEmpty(beginTime) && beginTime != null){
try {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(sdf.parse(beginTime));
c2.setTime(sdf.parse(endTime));
int newDay = c2.get(Calendar.DAY_OF_YEAR);
int oldDay = c1.get(Calendar.DAY_OF_YEAR);
int newHours = c2.get(Calendar.HOUR_OF_DAY);
int oldHours = c1.get(Calendar.HOUR_OF_DAY);
int newMins = c2.get(Calendar.MINUTE);
int oldMins = c1.get(Calendar.MINUTE);
int newSecond = c2.get(Calendar.SECOND);
int oldSecond = c1.get(Calendar.SECOND);
boolean flag =ifLeapYear(c1.get(Calendar.YEAR));
//时间秒计算
if(newSecond < oldSecond){
newSecond = 60 + newSecond;
newMins = newMins -1;
}
second = newSecond - oldSecond;
//时间分计算
if(newMins < oldMins){
newMins = 60 + newMins;
newHours = newHours-1;
}
minute = newMins-oldMins;
//时间时计算
if(newHours < oldHours){
newHours=24+newHours;
newDay= newDay-1;
}
hours=newHours-oldHours;
//天计算
if(newDay < oldDay){
//判断是否为闰年
if(flag){
newDay=366+newDay;
}else{
newDay=365+newDay;
}
}
days = newDay - oldDay;
} catch (ParseException e) {
e.printStackTrace();
}
if(0!=days){
result = String.valueOf(Math.abs(days))+"天";
}
if(0 != hours){
result += String.valueOf(Math.abs(hours))+"小时";
}
if(0 != minute){
result += String.valueOf(Math.abs(minute))+"分钟";
}
if(0 != second){
result += String.valueOf(Math.abs(second))+"秒";
}
}
return result;
}
public static void main(String[] args){
String start = "2019-09-29 15:11:49";
String end = "2019-10-08 14:39:44";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date startDate;
try {
startDate = df.parse(start);
Date endDate = df.parse(end);
int s = getWorkDays(startDate,endDate);
System.out.println("time*****:"+s+"\n");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取两个日期之间的工作日时间 1天3小时3分5秒
* @param startDay
* @param endDay
* @return
* @throws DataAccessException
* @throws SQLException
*/
public static String getWorkTime(Date startDay,Date endDay) throws DataAccessException, SQLException{
String timeStr = "";
// 参数为空直接返回
if(startDay==null || endDay==null){
return "";
}
Calendar sCal = Calendar.getInstance();
Calendar eCal = Calendar.getInstance();
sCal.setTime(startDay);
eCal.setTime(endDay);
if(1900==eCal.get(Calendar.YEAR)){
endDay = new Date();
eCal.setTime(endDay);
}
long stime = sCal.getTimeInMillis();
long etime = eCal.getTimeInMillis();
long a = stime%1000;
if(a!=0){
stime = stime-a;
sCal.setTimeInMillis(stime);
}
long b = etime%1000;
if(b!=0){
etime = etime-b;
eCal.setTimeInMillis(etime);
}
if(eCal.compareTo(sCal)<=0){//如果开始日期与结束日期一样,直接返回""
return "0秒";
} else{//当开始日期与结束日期不一样时
long time = 0;
Map<Calendar,String> map = getHolidayFlag(sCal.getTime(), eCal.getTime());
Calendar endCalFormat = Calendar.getInstance();
endCalFormat.clear();
endCalFormat.set(eCal.get(Calendar.YEAR), eCal.get(Calendar.MONTH), eCal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
while(sCal.getTimeInMillis()<=(endCalFormat.getTimeInMillis()+999)){
// 获取法定假日标志
Calendar calFormat = Calendar.getInstance();
calFormat.clear();
calFormat.set(sCal.get(Calendar.YEAR), sCal.get(Calendar.MONTH), sCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
String holidayFlag = map.get(calFormat);
if("0".equals(holidayFlag)){// 设为工作日,即使是周末也算工作日
if(startDay.equals(sCal.getTime())){//起始日是工作日
calFormat.add(Calendar.DATE, 1);
if(eCal.getTimeInMillis()-calFormat.getTimeInMillis()<=0){
time+=eCal.getTimeInMillis()-sCal.getTimeInMillis();
}else{
time+=calFormat.getTimeInMillis()-sCal.getTimeInMillis();
}
}else{
if(sCal.get(Calendar.YEAR)==eCal.get(Calendar.YEAR)&&sCal.get(Calendar.MONTH)==eCal.get(Calendar.MONTH)&&sCal.get(Calendar.DAY_OF_MONTH)==eCal.get(Calendar.DAY_OF_MONTH)){
time+=eCal.getTimeInMillis()-calFormat.getTimeInMillis();
}else{
time+=3600*24*1000;
}
}
}else if("1".equals(holidayFlag)){// 设为法定假日,即使是工作日也算假日
}else{// 普通的时间看是否是周末
// 获取是否周末
int day = sCal.get(Calendar.DAY_OF_WEEK);
if(day!=Calendar.SATURDAY&&day!=Calendar.SUNDAY){// 工作日
if(startDay.equals(sCal.getTime())){//起始日是工作日
calFormat.add(Calendar.DATE, 1);
if(eCal.getTimeInMillis()-calFormat.getTimeInMillis()<=0){
time+=eCal.getTimeInMillis()-sCal.getTimeInMillis();
}else{
time+=calFormat.getTimeInMillis()-sCal.getTimeInMillis();
}
}else{
if(sCal.get(Calendar.YEAR)==eCal.get(Calendar.YEAR)&&sCal.get(Calendar.MONTH)==eCal.get(Calendar.MONTH)&&sCal.get(Calendar.DAY_OF_MONTH)==eCal.get(Calendar.DAY_OF_MONTH)){
time+=eCal.getTimeInMillis()-calFormat.getTimeInMillis();
}else{
time+=3600*24*1000;
}
}
}else{// 周末
}
}
sCal.add(Calendar.DATE, 1);
}
int days = (int)(time/(3600*24*1000));
int remainder = (int)(time%(3600*24*1000));
int hours = (int)(remainder/(3600*1000));
remainder = (int)(remainder%(3600*1000));
int minutes = (int)(remainder/(60*1000));
remainder = (int)(remainder%(60*1000));
int seconds = (int)(remainder/(1000));
timeStr = (days!=0?(days+"天"):"")+(hours!=0?(hours+"小时"):"")+(minutes!=0?(minutes+"分"):"")+(seconds!=0?(seconds+"秒"):"");
}
if(StringUtilsCrew.isEmpty(timeStr)){
timeStr = "0秒";
}
return timeStr;
}
/**
* 获取两个日期之间的工作日时间 秒
* @param startDay
* @param endDay
* @return
* @throws DataAccessException
* @throws SQLException
*/
public static long getWorkTimeSecond(Date startDay,Date endDay) throws DataAccessException, SQLException{
long timeSecond = 0L;
// 参数为空直接返回
if(startDay==null || endDay==null){
return 0L;
}
Calendar sCal = Calendar.getInstance();
Calendar eCal = Calendar.getInstance();
sCal.setTime(startDay);
eCal.setTime(endDay);
if(1900==eCal.get(Calendar.YEAR)){
endDay = new Date();
eCal.setTime(endDay);
}
long stime = sCal.getTimeInMillis();
long etime = eCal.getTimeInMillis();
long a = stime%1000;
if(a!=0){
stime = stime-a;
sCal.setTimeInMillis(stime);
}
long b = etime%1000;
if(b!=0){
etime = etime-b;
eCal.setTimeInMillis(etime);
}
if(eCal.compareTo(sCal)<=0){//如果开始日期与结束日期一样,直接返回""
return 0L;
} else{//当开始日期与结束日期不一样时
long time = 0;
Map<Calendar,String> map = getHolidayFlag(sCal.getTime(), eCal.getTime());
Calendar endCalFormat = Calendar.getInstance();
endCalFormat.clear();
endCalFormat.set(eCal.get(Calendar.YEAR), eCal.get(Calendar.MONTH), eCal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
while(sCal.getTimeInMillis()<=(endCalFormat.getTimeInMillis()+999)){
// 获取法定假日标志
Calendar calFormat = Calendar.getInstance();
calFormat.clear();
calFormat.set(sCal.get(Calendar.YEAR), sCal.get(Calendar.MONTH), sCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
String holidayFlag = map.get(calFormat);
if("0".equals(holidayFlag)){// 设为工作日,即使是周末也算工作日
if(startDay.equals(sCal.getTime())){//起始日是工作日
calFormat.add(Calendar.DATE, 1);
if(eCal.getTimeInMillis()-calFormat.getTimeInMillis()<=0){
time+=eCal.getTimeInMillis()-sCal.getTimeInMillis();
}else{
time+=calFormat.getTimeInMillis()-sCal.getTimeInMillis();
}
}else{
if(sCal.get(Calendar.YEAR)==eCal.get(Calendar.YEAR)&&sCal.get(Calendar.MONTH)==eCal.get(Calendar.MONTH)&&sCal.get(Calendar.DAY_OF_MONTH)==eCal.get(Calendar.DAY_OF_MONTH)){
time+=eCal.getTimeInMillis()-calFormat.getTimeInMillis();
}else{
time+=3600*24*1000;
}
}
}else if("1".equals(holidayFlag)){// 设为法定假日,即使是工作日也算假日
}else{// 普通的时间看是否是周末
// 获取是否周末
int day = sCal.get(Calendar.DAY_OF_WEEK);
if(day!=Calendar.SATURDAY&&day!=Calendar.SUNDAY){// 工作日
if(startDay.equals(sCal.getTime())){//起始日是工作日
calFormat.add(Calendar.DATE, 1);
if(eCal.getTimeInMillis()-calFormat.getTimeInMillis()<=0){
time+=eCal.getTimeInMillis()-sCal.getTimeInMillis();
}else{
time+=calFormat.getTimeInMillis()-sCal.getTimeInMillis();
}
}else{
if(sCal.get(Calendar.YEAR)==eCal.get(Calendar.YEAR)&&sCal.get(Calendar.MONTH)==eCal.get(Calendar.MONTH)&&sCal.get(Calendar.DAY_OF_MONTH)==eCal.get(Calendar.DAY_OF_MONTH)){
time+=eCal.getTimeInMillis()-calFormat.getTimeInMillis();
}else{
time+=3600*24*1000;
}
}
}else{// 周末
}
}
sCal.add(Calendar.DATE, 1);
}
int days = (int)(time/(3600*24*1000));
int remainder = (int)(time%(3600*24*1000));
int hours = (int)(remainder/(3600*1000));
remainder = (int)(remainder%(3600*1000));
int minutes = (int)(remainder/(60*1000));
remainder = (int)(remainder%(60*1000));
int seconds = (int)(remainder/(1000));
//String timeStr = (days!=0?(days+"天"):"")+(hours!=0?(hours+"小时"):"")+(minutes!=0?(minutes+"分"):"")+(seconds!=0?(seconds+"秒"):"");
//System.out.println(timeStr);
timeSecond = time;
}
return timeSecond;
}
/**
* 获取节假日标志
* @param startDay
* @param endDay
* @return
* @throws DataAccessException
* @throws SQLException
*/
public static Map<Calendar,String> getHolidayFlag(Date startDay,Date endDay) throws DataAccessException, SQLException{
Map<Calendar,String> map = new HashMap<Calendar,String>();
JDBCDataSource datasource = JDBCDataSource.getJDBCDataSource();
PreparedStatement ps = null;
ResultSet rs = null;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String startDayStr = df.format(startDay);
String endDayStr = df.format(endDay);
String sql = "SELECT * FROM T_Dict_Holiday WHERE holiday_date BETWEEN to_date('"+startDayStr+"','yyyy-mm-dd') AND to_date('"+endDayStr+"','yyyy-mm-dd') and valid_flag='1'";
ps = datasource.getConnection().prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
String holidayFlag = rs.getString("holiday_flag");
Date holidayDate = rs.getDate("holiday_date");
Calendar cal = Calendar.getInstance();
cal.setTime(holidayDate);
map.put(cal, holidayFlag);
}
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
return map;
}
/**
* 是否过期n年,已经过期n年返回true,不是返回false
* @param year 过期的年数
* @param jzrq 证书截止日期
* @return
*/
public boolean ifSrzGq(int year,String jzrq){
Date date =new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
date.setYear(date.getYear()-year);
date.setMonth(date.getMonth());
date.setDate(date.getDate());
String d=df.format(date);
if(jzrq==null||StringUtilsCrew.isEmpty(jzrq)||"null".equals(jzrq.trim())){
return true;
}else{
try{
int jzrqint = Integer.valueOf(jzrq);
}catch(Exception e) {
try {
jzrq=df.format(new SimpleDateFormat("yyyy-MM-dd").parse(jzrq));
} catch (ParseException e1) {
e.printStackTrace();
}
}
if(Integer.valueOf(d)>Integer.valueOf(jzrq)){
return true;
}
}
return false;
}
}
/**
* 自定义日期工具类
*
*/
public class DateUtilsCrew {/**
* 计算两个日期之间相差的天数
* @param smdate 较小的时间
* @param bdate 较大的时间
* @return 相差天数
* @throws ParseException
*/
public static int daysBetween(Date smdate,Date bdate) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
smdate=sdf.parse(sdf.format(smdate));
bdate=sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 获得固定间隔的日期,如前60天dateAdd(-60),后60天dateAdd(60)
*
* @param sourceDate
* :某一日期
* @param amount
* :距某一日期的间隔日期长度,向前为负,向后为正
* @return 固定间隔的日期,格式yyyy-MM-dd
*/
public String dateAdd(String sourceDate, int amount) {
Calendar cal = new GregorianCalendar();
java.sql.Date trialTime = java.sql.Date.valueOf(sourceDate);
cal.setTime(trialTime);
cal.add(GregorianCalendar.DATE, amount);
int y = cal.get(GregorianCalendar.YEAR);
int m = cal.get(GregorianCalendar.MONTH) + 1;
int d = cal.get(GregorianCalendar.DAY_OF_MONTH);String ymd = Integer.toString(y) + "-" + Integer.toString(m) + "-"
+ Integer.toString(d);return dateFormat(ymd, "yyyy-MM-dd");
}/**
* 格式化日期显示格式
*
* @param sdate :
* 原始日期
* @param format
* :格式化后日期格式
* @return 格式化后的日期显示
*/
public static String dateFormat(String sdate, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
java.sql.Date date = java.sql.Date.valueOf(sdate);
String dateString = formatter.format(date);
return dateString;
}
/**
* 格式化日期显示格式
*
* @param date
* :原始日期
* @param format
* :格式化后日期格式
* @return 格式化后的日期显示
*/
public static String dateFormat(java.util.Date date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
String dateString = formatter.format(date);
return dateString;
}/**
* 自定义格式化日期串到日期类型。
* @param dateStr
* @param format
* @return
* @throws ParseException
*/
public static Date parsetoDate(String dateStr,String format) throws ParseException{
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.parse(dateStr);
}
/**
* 根据一个日期,取得之后n个月的日期 yyyy-MM-dd
*
* @param startDate
* @param monthNum
* @return
*/
public static Date getEndDate(Date startDate, int monthNum) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String startDateStr = sdf.format(startDate);int startYear = Integer.parseInt(startDateStr.substring(0, 4));
int startMonth = Integer.parseInt(startDateStr.substring(4, 6));
int startDay = Integer.parseInt(startDateStr.substring(6, 8));startMonth = startMonth + monthNum;
if (startMonth > 12) {
startYear = startYear + (startMonth / 12);
startMonth = startMonth % 12;
if (startMonth == 0) {
startYear = startYear - 1;
startMonth = 12;
}
}switch (startMonth) {
case 4:
if (startDay > 30) {
startDay = 30;
}
break;
case 6:
if (startDay > 30) {
startDay = 30;
}
break;
case 9:
if (startDay > 30) {
startDay = 30;
}
break;
case 11:
if (startDay > 30) {
startDay = 30;
}
break;case 2:
if (startDay > 28) {
if (ifLeapYear(startYear)) {
startDay = 29;
} else {
startDay = 28;
}
}
break;}
StringBuffer endDateStr = new StringBuffer();
endDateStr.append(startYear);
if (startMonth < 10) {
endDateStr.append("0");
}
endDateStr.append(startMonth);
if (startDay < 10) {
endDateStr.append("0");
}
endDateStr.append(startDay);Date endDate = sdf.parse(endDateStr.toString());
return endDate;
}
/**
* 判断年份是否为闰年 ------------------------------------------------- 润年的条件:
* 1)能被4整除,但不能被100整除的年份; 2)能被100整除,又能被400整除的年份;
* ------------------------------------------------- 设y为被检测的年份,则算法可表示如下:
* S2:若y不能被4整除,则输出n“不是闰年” S3:若y能被4整除,不能被100整除,则输出y“是闰年”
* S4:若y能被100整除,又能被400整除,输出y“是闰年” S5:输出y“不是闰年”
*
* @param y
* @return
*/
public static boolean ifLeapYear(int y) {
boolean bFlag = false;
if ((y % 4) != 0) {
bFlag = false;
} else if ((y % 100) != 0) {
bFlag = true;
} else if ((y % 400) == 0) {
bFlag = true;
} else {
bFlag = false;
}return bFlag;
}
/**
* 如果附件上传日期超过两年则已失效
*/
public static boolean overTwoYears(String date1, Date date2) throws Exception {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-M-d HH:mm:ss");
java.util.Date start = sdf.parse(date1);
long cha = date2.getTime() - start.getTime();
double result = cha * 1.0 / (1000 * 60 * 60);
if (result <= 17520) {
return true;
} else {
return false;
}
}
}