• 从当前日期算起,获取几天前的日期和几个月前的日期


    	/**
    	 * 从当前日期算起,获取N天前的日期(当前日不算在内),日期格式为yyyy-MM-dd
    	 * 
    	 * @param daily 天数
    	 * @return 
    	 */
    	public static String getDateByDay(Integer daily) {
    		Date date = new Date();
    		int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
    		int month = Integer.parseInt(new SimpleDateFormat("MM").format(date));
    		int day = Integer.parseInt(new SimpleDateFormat("dd").format(date)) - daily;
    		if (day < 1) {
    			month -= 1;
    			if (month == 0) {
    				year -= 1;
    				month = 12;
    			}
    			if (month == 4 || month == 6 || month == 9 || month == 11) {
    				day = 30 + day;
    			} else if (month == 1 || month == 3 || month == 5 || month == 7
    					|| month == 8 || month == 10 || month == 12) {
    				day = 31 + day;
    			} else if (month == 2) {
    				if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
    					day = 29 + day;
    				}
    				else {
    					day = 28 + day;
    				}
    			}
    
    		}
    		String y = year + "";
    		String m = "";
    		String d = "";
    		if (month < 10) {
    			m = "0" + month;
    		} else {
    			m = month + "";
    		}
    		if (day < 10) {
    			d = "0" + day;
    		} else {
    			d = day + "";
    		}
    		return y + "-" + m + "-" + d;
    	}
    
    	/**
    	 * 从当前日期算起,获取N个月前的日期,日期格式为yyyy-MM-dd
    	 * 
    	 * @param mon 月份
    	 * @return
    	 */
    	public static String getDateByMonth(Integer mon) {
    		Date date = new Date();
    		int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
    		int month = Integer.parseInt(new SimpleDateFormat("MM").format(date)) - mon;
    		int day = Integer.parseInt(new SimpleDateFormat("dd").format(date));
    		if (month == 0) {
    			year -= 1;
    			month = 12;
    		} else if (day > 28) {
    			if (month == 2) {
    				if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
    					day = 29;
    				} else {
    					day = 28;
    				}
    			} else if ((month == 4 || month == 6 || month == 9 || month == 11)
    					&& day == 31) {
    				day = 30;
    			}
    		}
    		String y = year + "";
    		String m = "";
    		String d = "";
    		if (month < 10) {
    			m = "00" + month;
    		} else {
    			m = month + "";
    		}
    		if (day < 10) {
    			d = "0" + day;
    		} else {
    			d = day + "";
    		}
    		return y + "-" + m + "-" + d;
    	}
    
  • 相关阅读:
    Python3安装turtle提示错误:Command "python setup.py egg_info" failed with error code 1
    慕学生鲜xadmin登录不成功解决办法
    mysql删除后ID不连续解决办法
    Mac 安装gevent
    ubuntu 下 docker的简单使用
    python 字典的一些简单操作
    列表推导式
    Serializer字段和选项
    添加包解决一些导包问题
    css 文本换行,文本空白符处理相关 属性:white-space word-wrap word-break
  • 原文地址:https://www.cnblogs.com/linjiqin/p/2276467.html
Copyright © 2020-2023  润新知