JAVA 求当前日期的下一天、下一周、下一个月,向前求解也类似

JAVA中时间格式很多,在求解时,比较方便调用

1、求解下一天

public static String getNextDay(String now){
		Date nowdate;
		String nday=new String();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");

		try{
			nowdate=sdf.parse(now);
			long oneday=1000*3600*24;
			Date nextday=new Date(nowdate.getTime()+oneday);
			nday = sdf.format(nextday.getTime());

		}catch(Exception e ){

			e.printStackTrace();
		}
		return nday;
	}

主要用到的是JAVA中Date类型获取毫秒数

2、求解一周

public static String getNextWeek(String now){
		Date nowdate;
		String nweek=new String();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");

		try{
			nowdate=sdf.parse(now);
			
			long oneday=1000*3600*24;
			Date nextweek=new Date(nowdate.getTime()+oneday*(9-nowdate.getDay()));
			nweek = sdf.format(nextweek.getTime());

		}catch(Exception e ){

			e.printStackTrace();
		}
		return nweek;
	}
3、求解下一个月


   

public static String getNextMonth(String now){
		Date t;
		String nmonth=new String();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMM");

		try{
			t=sdf.parse(now);
			Date nextmonth=new Date((t.getYear()+(t.getMonth()+1)/12),(t.getMonth()+1)%12,t.getDate());
			nmonth = sdf.format(nextmonth.getTime());

		}catch(Exception e ){

			e.printStackTrace();
		}
		return nmonth;
	}

4、求解下一个
public static String getNextHour(String now){
		Date nowdate;
		String nhour=new String();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd HH");

		try{
			nowdate=sdf.parse(now);
			long onehour=1000*3600;
			Date nextday=new Date(nowdate.getTime()+onehour);
			nhour= sdf.format(nextday.getTime());

		}catch(Exception e ){

			e.printStackTrace();
		}
		return nhour;
	}

你可能感兴趣的:(java)