@echo off
set jcrq=2018-06-18
set today=%date:~,10%
echo wscriptecho DateDiff("d",%today%,%jcrq%)>avbs
for /f %%i in ('cscript /nologo avbs') do set days=%%i
echo %jcrq% 与 %today% 相差 %days% 天。
del avbs
pause
迟来的答案
1周末版本(不含节假日判断)
注意:最下面是使用的 递归算法
/获得收益时间(获取当前天+1天,周末不算)
@param date
任意日期
@return the income date
@throws NullPointerException
if null == date
/
private Date getIncomeDate(Date date) throws NullPointerException{
if (null == date){
throw new NullPointerException("the date is null or empty!");
}
//对日期的 *** 作,我们需要使用 Calendar 对象
Calendar calendar = new GregorianCalendar();
calendarsetTime(date);
//+1天
calendaradd(CalendarDAY_OF_MONTH, +1);
//判断是星期几
int dayOfWeek = calendarget(CalendarDAY_OF_WEEK);
Date incomeDate = calendargetTime();
if (dayOfWeek == 1 || dayOfWeek == 7){
//递归
return getIncomeDate(incomeDate);
}
return incomeDate;
}
测试方法:
@Testpublic void testGetIncomeDate() throws ParseException{
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Systemoutprintln(simpleDateFormatformat(getIncomeDate(new Date())));
Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-07-31 13:33:05"))));
Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-01 13:33:05"))));
Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-02 13:33:05"))));
Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-03 13:33:05"))));
}
输出结果:
2014-08-01 13:48:092014-08-01 13:33:05
2014-08-04 13:33:05
2014-08-04 13:33:05
2014-08-04 13:33:05
注意:返回的是 时间+1的时间,精度是到毫秒 纳秒,如果有特殊需求,需要自己再处理下
2周末+节假日版本
/获得收益时间(获取当前天+1天,周末不算)
@param date
任意日期
@return the income date
@throws NullPointerException
if null == date
/
private Date getIncomeDate(Date date) throws NullPointerException{
if (null == date){
throw new NullPointerException("the date is null or empty!");
}
//对日期的 *** 作,我们需要使用 Calendar 对象
Calendar calendar = new GregorianCalendar();
calendarsetTime(date);
//+1天
calendaradd(CalendarDAY_OF_MONTH, +1);
Date incomeDate = calendargetTime();
if (isWeekend(calendar) || isHoliday(calendar)){
//递归
return getIncomeDate(incomeDate);
}
return incomeDate;
}
/
判断一个日历是不是周末
@param calendar
the calendar
@return true, if checks if is weekend
/
private boolean isWeekend(Calendar calendar){
//判断是星期几
int dayOfWeek = calendarget(CalendarDAY_OF_WEEK);
if (dayOfWeek == 1 || dayOfWeek == 7){
return true;
}
return false;
}
/
一个日历是不是节假日
@param calendar
the calendar
@return true, if checks if is holiday
/
private boolean isHoliday(Calendar calendar){
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String dateString = simpleDateFormatformat(calendargetTime());
//节假日 这个可能不同地区,不同年份 都有可能不一样,所以需要有个地方配置, 可以放数据库, 配置文件,环境变量 等等地方
//这里以配置文件 为例子
ResourceBundle resourceBundle = ResourceBundlegetBundle("holidayConfig");
String holidays = resourceBundlegetString("holiday");
String[] holidayArray = holidayssplit(",");
boolean isHoliday = orgapachecommonslangArrayUtilscontains(holidayArray, dateString);
return isHoliday;
}
配置文件:
holiday=2014-10-01,2014-10-02,2014-10-03,2014-10-04,2014-10-05,2014-10-06,2014-10-07测试方法 :
/Testenclosing_type
@throws ParseException
the parse exception
/
@Test
public void testGetIncomeDate() throws ParseException{
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Systemoutprintln(simpleDateFormatformat(getIncomeDate(new Date())));
Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-07-31 13:33:05"))));
Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-01 13:33:05"))));
Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-02 13:33:05"))));
Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-03 13:33:05"))));
Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-09-30 13:33:05"))));
Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-10-02 13:33:05"))));
}
结果:
2014-08-01 15:14:592014-08-01 13:33:05
2014-08-04 13:33:05
2014-08-04 13:33:05
2014-08-04 13:33:05
2014-10-08 13:33:05
2014-10-08 13:33:05
以上就是关于cmd变量检测日期并计算出与预定日期差多少天全部的内容,包括:cmd变量检测日期并计算出与预定日期差多少天、用java 进行日期计算,获取当前天+1天,周末节假日不算,在线等待中,十万火急……、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)