Java gets the day before and after the specified date, the first day of the month, and the last day of the month

1. Get the time of the day before and the day after the specified date

The specified date can be today or a certain day

Get the day before and the day after today

Get today's date:Date date=new Date();

Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
//拿到前一天
calendar.add(Calendar.DAY_OF_MONTH,-1);
//获取后一天
calendar.add(Calendar.DAY_OF_MONTH, 1);
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(calendar.getTime());

Get the day before the specified date

Pass calendar.add(Calendar.DAY_OF_MONTH,-1)to get the date of the previous day

/**
	@param: 入参是当前时间2020-03-01
	@return:返参是前一天的日期,理应为2020-02-29(闰年)
*/
private String getBeforeDay(String dateTime){
    
    
   	SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
   	Date date = null;
	try{
    
    
       	date=simpleDateFormat.parse(dateTime);
    }catch (ParseException e){
    
    
        logger.error("String转Date日期失败:",e);
    }
	Calendar calendar=Calendar.getInstance();
	calendar.setTime(date);
	//往前一天
	calendar.add(Calendar.DAY_OF_MONTH,-1);
	return simpleDateFormat.format(calendar.getTime());
}

Get the day after the specified date

Contrary to the day before the acquisition:calendar.add(Calendar.DAY_OF_MONTH,1)

/**
	@param: 入参是当前时间2020-02-29
	@return:返参是后一天的日期,理应为2020-03-01
*/
private String getNexteDay(String dateTime){
    
    
   	SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
   	Date date = null;
	try{
    
    
       	date=simpleDateFormat.parse(dateTime);
    }catch (ParseException e){
    
    
        logger.error("String转Date日期失败:",e);
    }
	Calendar calendar=Calendar.getInstance();
	calendar.setTime(date);
	//往后一天
	calendar.add(Calendar.DAY_OF_MONTH,1);
	return simpleDateFormat.format(calendar.getTime());
}

2. Date format conversion: yyyy-MM to yyyyMM

The following operation will convert String type 2020-12 to 202012, and vice versa.

//将2020-12 转换为 202012
private String dateFormatChange(String dateTime){
    
    
	//入参的日期格式:yyyy-MM
	SimpleDateFormat simpleDateFormatInput=new SimpleDateFormat("yyyy-MM");
	Date date=null;
	//返参的日期格式: yyyyMM
	SimpleDateFormat simpleDateFormatOutput=new SimpleDateFormat("yyyyMMdd");
	try{
    
    
		//Date型 yyyy-MM
		date = simpleDateFormatInput.parse(dateTime);
	}catch (ParseException e){
    
    
		logger.error("日期格式转换失败(2020-11 => 20201102):",e);
	}
	return simpleDateFormatOutput.format(date);
}

3. Natural month calculation

For example: a consumer red envelope is issued on December 15, 2020, and the validity period is one month, then the red envelope will expire at 23:59 on December 31. Instead of expiring on January 15, 2021

If we want to judge whether this red envelope is now expired, then we have to get it until the last day of December 2020

Get the date of the last day of [the current month/a certain month]

Thinking: getActualMaximum(Calendar.DAY_OF_MONTH)can get a month's maximum number of days , as the last day of the month

/**
	@param dateTime: 发放生效日期2020-12-15
	@param expire:有效期 1个月
	@return: 获得失效日期 即当月的最后一天, 入参是2020-12-15,它所在的月的最后一天是31号
*/ 
private String calculateEndDate(String dateTime,int expire){
    
    
	Calendar calendar=Calendar.getInstance();
	Date date=null;
	SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
	try{
    
    
		date=simpleDateFormat.parse(dateTime) ;
	}catch(ParseException e){
    
    
		logger.error("String =>Date格式转换失败",e);
	}
	calendar.setTime(date);
	//自然月的计算规则,2020-12-15发放,有效期1个月,那过期时间是12月31号
	calendar.add(Calendar.MONTH,expire-1);
	//getActualMaximum(Calendar.DAY_OF_MONTH) 得到了31天
	calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
	//获得当月的最后一天日期 2020-12-31
	String endDate=simpleDateFormat.format(calendar.getTime());
	return endDate;
}

Get the first day of the month

This is generally used in timed tasks, such as reconciliation on the first day of the month

Ideas:

  • Get the minimum number of days in the month :calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH))
  • Direct set:calendar.set(Calendar.DAY_OF_MONTH, 1)
/**
	@param: 指定的日期,如 2020-12-29
	@return: 返回当月的第一天,应为2020-12-01
*/
private String getFirstDay(String dateTime){
    
    
	SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
	Date date = null;
	try{
    
    
        date=simpleDateFormat.parse(dateTime);
    }catch (ParseException e){
    
    
        logger.error("String转Date日期失败:",e);
    }
    Calendar calendar=Calendar.getInstance();
    calendar.setTime(date);
    /**
    	1. getActualMinimum(Calendar.DAY_OF_MONTH)是拿到当月最小天数作为第一天
    	
    	2. calendar.set(Calendar.DAY_OF_MONTH, 1)也能获取当月第一天
    */
    calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
    return simpleDateFormat.format(calendar.getTime());
}

Guess you like

Origin blog.csdn.net/qq_44384533/article/details/110133242