JDK8 adds a series of time classes,
(supposedly) to kill the Date,calendar of the past,
The past date class (supposedly) has many drawbacks, such as thread insecurity,
As for my personal feeling is really very troublesome to use, I generally encapsulated into a number of commonly used methods after each adjustment method, no longer want to see how the inside is realized.
After discovering this new class of LocalDateTime, after my simple trial, I feel that I can completely replace all the methods of using time before. Very good, very useful.
The following is a simple tutorial to use:
1. Get the current time in free form
Public Static string Gettodaybyformat (String timeformat) { return localdatetime.now (). Format ( Datetimeformatter.ofpattern (TimeFormat)); }
Almost elegant, even I do not want to encapsulate into a method, want to write at any time, it is so easy.
2, get current year/month/day
A picture shows, how to take, how to spell, all casually
3, get the day before
In the past my stupid method was:
Public Staticstring Getyesterdaybyformat (String timeformat) {//Get Current dateDate Date =NewDate (); SimpleDateFormat SF=NewSimpleDateFormat (TimeFormat); //Get next day's date through seconds LongTime = (Date.gettime ()/1000)-60 * 60 * 24;//secondsDate.settime (time * 1000);//millisecondsString yesterday =Sf.format (date). toString (); returnyesterday; }
Now there's a new way:
Public Static string Getyesterdaybyformat (String timeformat) { return localdatetime.now (). Plusdays (1). Format (Datetimeformatter.ofpattern (TimeFormat)); }
In the same way, I don't want to write the method separately for x days/hours, just below (as an example of an increase):
The return value is localdatetime, it means that you can always chain the point down, want to how many days after the data despite opening, a sentence to write I lose.
4, gets the milliseconds/second spent
Why one is to one is get I don't quite understand
Other more I still useless, feel not to use, there is need to check it again.
Java localdatetime,datetimefomatter----JDK8 simple use of new time classes