Online search for half a day did not find Java8 LocalDateTime tool class, had to write a, commonly used basic functions have. you're still using the date Java Fellow for a change.
Personal item Address: There are many practical toolkits in the Https://github.com/KingBoyWorld/common.git,Common module, all of which are optimized.
Tool class
Package com.kingboy.common.utils.date;
Import java.time.*;
Import Java.time.format.DateTimeFormatter;
Import Java.time.temporal.ChronoUnit;
Import Java.time.temporal.TemporalUnit;
Import Java.util.Date; * * @author kingboy * @Date 2017/7/22 pm 2:12 * @Description localdatetimeutils is used to Java8 time class */public class
Localdatetimeutils {//Get the LocalDateTime object of the current time//localdatetime.now ();
Build LocalDateTime//localdatetime.of () according to the date of year; Comparison date successively//localdatetime.now (). Isbefore (),//localdatetime.now (). Isafter (),//date convert to LocalDateTime public Static LocalDateTime Convertdatetoldt (date date) {return localdatetime.ofinstant (Date.toinstant (), Zoneid.system
Default ()); //localdatetime Convert to date public static date convertldttodate (LocalDateTime time) {return date.from.
Atzone (Zoneid.systemdefault ()). Toinstant ()); }//Gets the millisecond public static Long Getmillibytime (LocalDateTime time) of the specified date {return Time.atzonE (Zoneid.systemdefault ()). Toinstant (). Toepochmilli (); ///Gets the second public static Long Getsecondsbytime (LocalDateTime time) of the specified date {return Time.atzone (zoneid.systemde
Fault ()). Toinstant (). Getepochsecond (); //Gets the specified format of the specified time public static String formattime (LocalDateTime time,string pattern) {return Time.format (D
Atetimeformatter.ofpattern (pattern)); //Gets the specified format of the current time public static string Formatnow (string pattern) {return formattime (Localdatetime.now ()),
pattern); //date plus a number, depending on field and different values, field is chronounit.* public static LocalDateTime plus (LocalDateTime time, long, Te
Mporalunit field) {return Time.plus (number, field); //Date minus a number, the field parameter is chronounit.* public static LocalDateTime Minu (LocalDateTime time, long, if different values are subtracted from field),
Temporalunit field) {return Time.minus (Number,field); /** * Get two-date Difference field parameter for chronounit.* * @param starttime * @param endtime *@param field unit (minutes per month) * @return/public static long Betweentwotime (LocalDateTime starttime, Localdateti Me Endtime, chronounit field) {Period Period = Period.between (Localdate.from (starttime), Localdate.from (Endtime))
;
if (field = = Chronounit.years) return period.getyears ();
if (field = = chronounit.months) return Period.getyears () * + period.getmonths ();
Return Field.between (StartTime, Endtime); ///Get start of day, 2017,7,22 00:00 public static LocalDateTime Getdaystart (LocalDateTime times) {return TIME.W
Ithhour (0). Withminute (0). Withsecond (0). Withnano (0); //Get the end of day, 2017,7,22 23:59:59.999999999 public static LocalDateTime Getdayend (LocalDateTime time) {RE Turn time.withhour. Withminute (Withsecond). Withnano (999999999
);
}
}
Test class
Package com.kingboy.common.localdatetimeutils;
Import Com.kingboy.common.utils.date.LocalDateTimeUtils;
Import Org.junit.Test;
Import Java.time.LocalDateTime;
Import Java.time.temporal.ChronoUnit;
Import static com.kingboy.common.utils.date.LocalDateTimeUtils.getDayEnd;
Import static Com.kingboy.common.utils.date.LocalDateTimeUtils.getDayStart; /** * @author kingboy * @Date 2017/7/22 pm 7:16 * @Description locadatetimeutilstest is used to test localdatetime tool */PU Blic class Locadatetimeutilstest {@Test public void Format_test () {System.out.println (localdatetimeutil
S.formatnow ("YYYY year mm month DD Day HH:MM:SS"));
@Test public void Betweentwotime_test () {LocalDateTime start = Localdatetime.of (1993, 10, 13, 11, 11);
LocalDateTime end = Localdatetime.of (1994, 11, 13, 13, 13);
System.out.println ("Year:" + localdatetimeutils.betweentwotime (start, End, Chronounit.years)); System.out.println ("Month:" + localdatetimeutils.betweentwotime) (start, end, chronounit.months));
System.out.println ("Day:" + localdatetimeutils.betweentwotime (start, End, chronounit.days));
System.out.println ("Half-day:" + localdatetimeutils.betweentwotime (start, End, chronounit.half_days));
System.out.println ("Hour:" + localdatetimeutils.betweentwotime (start, End, chronounit.hours));
System.out.println ("minutes:" + localdatetimeutils.betweentwotime (start, End, chronounit.minutes));
System.out.println ("SEC:" + localdatetimeutils.betweentwotime (start, End, chronounit.seconds));
System.out.println ("Millisecond:" + localdatetimeutils.betweentwotime (start, End, Chronounit.millis));
//=============================================================================================
/*
Year: January: 13th: 396
Half-day: 792 Hours: 9506 Minutes: 570,362 seconds: 34221720 MS: 342217200 */} @Test public void Plus_test () {//Add 20 minutes System.out.println (localdatetimeutil S.formattime (Localdatetimeutils.plus (Localdatetime.now (), chronounit.minutes), "yyyy years m
M month DD Day hh:mm "));
Increased by two years System.out.println (Localdatetimeutils.formattime (Localdatetimeutils.plus) (Localdatetime.now (),
2, Chronounit.years), "yyyy year mm month DD Day hh:mm"));
//=============================================================================================
/*
July 22, 2017 22:53 July 22, 2019 22:33 */}
@Test public void Daystart_test () {System.out.println (Getdaystart (Localdatetime.now ()));
System.out.println (Getdayend (Localdatetime.now ()));//=============================================================================================
/*
2017-07-22t00:00 2017-07-22t23:59:59.999999999 */}
}