文章目录
一、获取指定日期的前一天、后一天的时间拿到今天的前一天、后一天获取指定日期的前一天获取指定日期的后一天二、日期格式转换:yyyy-MM转为yyyyMM三、自然月计算获取 [ 当月 /某个月] 的最后一天日期获得当月的第一天一、获取指定日期的前一天、后一天的时间
指定日期可以是今天或者某一天
拿到今天的前一天、后一天
拿到今天的日期: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());
获取指定日期的前一天
通过calendar.add(Calendar.DAY_OF_MONTH,-1)
来得到前一天的日期
/**@param: 入参是当前时间-03-01@return:返参是前一天的日期,理应为-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());}
获取指定日期的后一天
跟获取前一天相反:calendar.add(Calendar.DAY_OF_MONTH,1)
/**@param: 入参是当前时间-02-29@return:返参是后一天的日期,理应为-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());}
二、日期格式转换:yyyy-MM转为yyyyMM
下面的操作会将String型 -12 转换为20,反之改改也能用
//将-12 转换为 20private String dateFormatChange(String dateTime){//入参的日期格式:yyyy-MMSimpleDateFormat simpleDateFormatInput=new SimpleDateFormat("yyyy-MM");Date date=null;//返参的日期格式: yyyyMMSimpleDateFormat simpleDateFormatOutput=new SimpleDateFormat("yyyyMMdd");try{//Date型 yyyy-MMdate = simpleDateFormatInput.parse(dateTime);}catch (ParseException e){logger.error("日期格式转换失败(-11 => 2002):",e);}return simpleDateFormatOutput.format(date);}
三、自然月计算
举个例子:消费红包在12月15日发放,有效期一个月,那么该红包将会在12月31日23:59过期。而不是在1月15日过期
如果我们要判断这个红包现在是否过期,那么就要拿到12月的最后一天
获取 [ 当月 /某个月] 的最后一天日期
思路:getActualMaximum(Calendar.DAY_OF_MONTH)
能够拿到一个月的最大天数,可作为一个月的最后一天
/**@param dateTime: 发放生效日期-12-15@param expire:有效期 1个月@return: 获得失效日期 即当月的最后一天, 入参是-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);//自然月的计算规则,-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));//获得当月的最后一天日期 -12-31String endDate=simpleDateFormat.format(calendar.getTime());return endDate;}
获得当月的第一天
这个一般是在定时任务里面会用到,比如当月第一天对账
思路:
获得当月最小天数:calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH))
直接set :calendar.set(Calendar.DAY_OF_MONTH, 1)
/**@param: 指定的日期,如 -12-29@return: 返回当月的第一天,应为-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());}