Recently, I saw a picture in the wechat group of fans of Java Technology Stack’s wechat public account, saying that I was fired just after writing the following code.

Did you get fired because you didn’t write notes?

Obviously not, sleep logic, we all understand, do not need to write a comment, you comment write sleep for 1 day is meaningless ah…

This programmer’s mind is not ordinary cattle ah, to obtain the next day’s date, unexpectedly to sleep until the next day to obtain, crying no tears…

Take a look at the enhanced version to get dates for the next few days…

* @param days Specifies the days * @author wechat public id: Java technology stack * @return*/ public static Date getNextDays(int days){try {public static Date getNextDays(int days){thread. sleep(days * 24 * 60 * 60 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } // Returns the date after hibernation endsreturn new Date();
}
Copy the code

Finished writing, has been laughing dizzy in the toilet…

Can’t find an excuse to leave?

That’s not easy. I send you this enhanced version, copy the code, submit the package test, go live, and get fired.

Having said all that, what’s the correct pose for picking up future dates?

public static Date getNextDays(int days) {
	Calendar c = Calendar.getInstance();
	c.setTime(new Date());
	c.add(Calendar.DATE, days);
	return c.getTime();
}
Copy the code

It is recommended to use The Apache Commons-lang utility class, there is no need to duplicate the wheel, it is not as good as others…

org.apache.commons.lang3.time.DateUtils#addDays

The principle also uses the Calendar class in Java to process.

Follow the wechat public account of Java technology stack, stack leader will continue to share Java dry goods tutorial, the public account will be the first time to push, continue to pay attention to. In the public account background reply: Java, get stack length arrangement of more Java tutorial, are actual combat dry goods, the following is only part of the preview.

  • Do you really understand the transient keyword?
  • 1. Synchronized?
  • With Java 11 released, Strings can still play like this!
  • Are Strings in Java really immutable?
  • Five differences between sleep() and wait()