部分和时间相关的方法

这篇博客分享了JavaScript和Java中处理日期和时间的方法。包括JavaScript获取上个月第一天和最后一天、当前日期和时间的函数,以及比较两个日期先后顺序的函数。而在Java部分,展示了如何拼接SQL查询条件以及生成当前日期后七天的时间。这些函数对于日常开发中的时间处理非常实用。
摘要由CSDN通过智能技术生成

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语句
    /**
     * 生成sql语句
     * @param publishStartTime
     * @param publishEndTime
     * @return
     */
    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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值