获取当前时间的前一天和后一天
程序员文章站
2024-03-17 14:39:04
...
package jp.co.hitachi_solutions.regasas.common;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* DateUtil
*/
public class DateUtil {
/**
* 当前日期的前一天
* @param specifiedDay
* @return
* @throws Exception
*/
public static String getSpecifiedDayBefore(String specifiedDay){
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day-1);
String dayBefore=new SimpleDateFormat("yyyyMMdd").format(c.getTime());
return dayBefore;
}
/**
* 当前日期的后一天
* @param specifiedDay
* @return
*/
public static String getSpecifiedDayAfter(String specifiedDay){
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day+1);
String dayAfter=new SimpleDateFormat("yyyyMMdd").format(c.getTime());
return dayAfter;
}
}
上一篇: 小数进制转换
下一篇: 二分查找(函数实现)