js
部分获取年月日的方法
1. 获取当前日期上一个月第一天
function getPreMonthTime(){
var dayTime = new Date();
dayTime.setTime(dayTime.getTime());
var yearVal = dayTime.getFullYear();
var monthVal = dayTime.getMonth();
if (monthVal == 0) {
yearVal = yearVal-1;
monthVal = 12;
}
var curentTime = yearVal+"-" + monthVal + "-" + "01";
return curentTime;
}
2. 获取上一个月最后一天
function getPreMonthLastTime(){
var dayTime = new Date();
dayTime.setTime(dayTime.getTime());
var yearVal = dayTime.getFullYear();
var monthVal = dayTime.getMonth();
if (monthVal == 0) {
yearVal = yearVal-1;
monthVal = 12;
}
var lastDay = new Date(yearVal, monthVal, 0);
var curentTime = lastDay.getFullYear()+"-" + (lastDay.getMonth()+1) + "-" + lastDay.getDate();
return lastDay;
}
3. 获取当前日
function getCurrentDay(){
var mydate = new Date().getDate();
return mydate;
}
4. 获取当前月
function getCurrentMonth(){
var mydate = new Date().getMonth()+1;
return mydate;
}
5. 比较两个时间前后顺序
function CompareDate(d1, d2) {
return ((new Date(d1.replace(/-/g, "\/"))) > (new Date(d2.replace(/-/g, "\/"))));
}
var startReviewTime = "2022-10-11";
var endReviewTime = "2022-10-11";
if (new Date(startReviewTime).getTime() > new Date(endReviewTime).getTime()) {
return;
}
6. 获取当前日期
function getCurrentTime(){
var dayTime = new Date();
dayTime.setTime(dayTime.getTime());
var curentTime = dayTime.getFullYear()+"-" + (dayTime.getMonth()+1) + "-" + dayTime.getDate();
return curentTime;
}
7. js比较两个时间的大小
function compareDate(date1, date2) {
var oDate1 = new Date(date1);
var oDate2 = new Date(date2);
if (oDate1.getTime() > oDate2.getTime()) {
return true;
} else {
return false;
}
}
Java
1. java拼接两个时间的sql语句
private StringBuffer getSqlCondition(String publishStartTime,String publishEndTime)
{
StringBuffer sqlCondition = new StringBuffer();
sqlCondition.append(" 1=1");
if (!BwFunc.checkNullOrEmpty(publishStartTime) && !BwFunc.checkNullOrEmpty(publishEndTime) && !publishStartTime.equals(publishEndTime))
{
sqlCondition.append(" and PUB_TIME >= '"+publishStartTime+"' and PUB_TIME <= '"+publishEndTime+"'");
} else if (!BwFunc.checkNullOrEmpty(publishStartTime) && BwFunc.checkNullOrEmpty(publishEndTime))
{
sqlCondition.append(" and PUB_TIME >= '"+publishStartTime+"'");
}else if (BwFunc.checkNullOrEmpty(publishStartTime) && !BwFunc.checkNullOrEmpty(publishEndTime))
{
sqlCondition.append(" and PUB_TIME <= '"+publishEndTime+"'");
}else if (!BwFunc.checkNullOrEmpty(publishStartTime) && !BwFunc.checkNullOrEmpty(publishEndTime) && publishStartTime.equals(publishEndTime))
{
sqlCondition.append(" and PUB_TIME >= '"+publishStartTime+" 00:00:00' and PUB_TIME <= '"+publishEndTime+" 23:59:59'");
}
return sqlCondition;
}
2.生成当前包含当前时间的七天后的时间
Date currentTime = new Date();
System.out.println("当前的时间是:"+sdf.format(currentTime));
Date endSignTime = DateUtils.addDays(currentTime, 6);
System.out.println("七天后的时间是:"+sdf.format(DateUtils.addDays(currentTime, 7)));
结果:
当前的时间是:2022-06-10
七天后的时间是:2022-06-17