JAVA asks the current date next day, next week, next one months, the forward solution also resembles __java

Source: Internet
Author: User

Java in a lot of time format, in the solution, more convenient to call

1, the next day to solve

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;
	}

The main use is the date type in Java to get the number of milliseconds

2. Solve a week

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, solve the next one months



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. Solve the next
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;
	}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.