- package dateTest;
- import java.text.ParsePosition;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- public class DateIncreaseOrReduce {
- /**
- * @param
- * @return void
- */
- public static void main(String[] args) {
- SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
- System.out.println(format.format(new Date()));
- int amount = -3;
- int filed = Calendar.DAY_OF_MONTH;
- Date temp = getIncreaseOrReduceDate(new Date(), filed, amount);
- System.out.println(format.format(temp));
- }
- //根据传入时间,来返回传入时间大于或者小于N天后的日期
- public static Date getIncreaseOrReduceDate(Object ob, int field, int amount){
- Date dateTemp = null;
- if(ob instanceof String){
- dateTemp = getDate(ob.toString(), "yyyyMMdd");
- }else if(ob instanceof Date){
- dateTemp = (Date)ob;
- }else{
- return null;
- }
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(dateTemp);
- calendar.add(field, amount);
- return calendar.getTime();
- }
- public static Date getDate(String sDate, String dateFormat) {
- SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
- ParsePosition pos = new ParsePosition(0);
- return fmt.parse(sDate, pos);
- }
- }
日期小玩意
最新推荐文章于 2022-08-25 16:21:21 发布