jdk1.8 time tools

Final static String = DEFAULT_DATE_FORMAT public "the MM-dd-YYYY"; 
public static String DEFAULT_TIME_FORMAT Final = "HH: mm: SS";
public static String DEFAULT_DATETIME_FORMAT = Final "the MM-dd-YYYY HH: mm: SS";

/ **
* date formatted string, using default the mM-dd-HH YYYY: mm: SS
* @param LocalDateTime
* @return formatDateToString
* /
public static string formatDateToString (the LocalDateTime LocalDateTime) {
return localDateTime.format (DateTimeFormatter.ofPattern (DEFAULT_DATETIME_FORMAT ));
}

/ **
* the given format to convert time to a string
* @param localDateTime be formatted date
* @param format conversion format
* @return formatDateToString
* /
formatDateToString static String public (the LocalDateTime LocalDateTime, the format String) {
IF (= null LocalDateTime!) {
return localDateTime.format (DateTimeFormatter.ofPattern (the format));
} the else {
return null;
}
}

/ **
* characters are converted to time, default yyyy-mM-dd HH: mm : ss format
* @param DATE
* @return the LocalDateTime
* /
public static formatStringToDate the LocalDateTime (String DATE) {
the DateTimeFormatter DateTimeFormatter = DateTimeFormatter.ofPattern (DEFAULT_DATETIME_FORMAT);
return LocalDateTime.parse (DATE, DateTimeFormatter );
}

/ **
* characters are converted to time, default yyyy-mM-dd HH: mm : ss format
* @param data date string
* @param format conversion format
* @return LocalDateTime
*/
public static LocalDateTime formatStringToDate(String data,String format){
if (null != data && ! data.equals("")){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
return LocalDateTime.parse(data,dateTimeFormatter);
} else {
return null;
}
}

/**
* 将LocalDateTime转为long类型的timestamp
* @param localDateTime
* @return long
*/
public static long getTimestampOfDateTime(LocalDateTime localDateTime){
ZoneId zoneId = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zoneId).toInstant();
return instant.toEpochMilli();
}

/ **
* The long type timestamp into the LocalDateTime
* @param timestamp
* @return the LocalDateTime
* /
public static getDateTimeOfTimestamp the LocalDateTime (long timestamp) {
the Instant Instant Instant.ofEpochMilli = (timestamp);
ZoneID ID of ZoneID ID of ZoneId.systemDefault = () ;
return LocalDateTime.ofInstant (Instant, ZoneID ID of);
}

/ **
* determines whether a date after the specified date
* @param data to compare the date
* @param afterData specified date
* @param format format
* @return Boolean
* /
static Boolean isAfter public (Data String, String afterData, the format String) {
the DateTimeFormatter DateTimeFormatter = DateTimeFormatter.ofPattern (the format);
DATAS = LocalDateTime.parse the LocalDateTime (Data, DateTimeFormatter);
the LocalDateTime afterDa = LocalDateTime.parse (afterData, DateTimeFormatter);
return datas.isAfter (afterDa);
}

/ **
* determines a date is before the specific date
* @param data to comparison date
* @param before the specified date
* @param format format
* @return Boolean
* /
public static Boolean isBefore (Data String, String before, the format String) {
the DateTimeFormatter DateTimeFormatter = DateTimeFormatter.ofPattern (the format);
the LocalDateTime = DATAS the LocalDateTime .parse (Data, DateTimeFormatter);
the LocalDateTime afterDa = LocalDateTime.parse (before, DateTimeFormatter);
return datas.isBefore (afterDa);
}

/ **
* Determining whether a leap year
* @param data date format: the MM-dd-YYYY
* @return Boolean
* /
public static Boolean isLeapYear (String Data) {
the LocalDate Yeat = LocalDate.parse (Data);
return yeat.isLeapYear ();
}

/ **
* is determined whether or not a leap year
* @param localDate date format: the MM-dd-YYYY
* @return Boolean
* /
public static Boolean isLeapYear (the localDate localDate) {
return localDate.isLeapYear ();
}

/ **
* current time plus in Save
* @param years years
* @return
* /
public static plusYears the LocalDateTime (int years) {
the LocalDateTime LocalDateTime.now LocalDateTime = ();
return localDateTime.plusYears (years);
}

/ **
* add or subtract the current month
* @param months month
* @return
* /
public static plusMonths the LocalDateTime (int months) {
the LocalDateTime LocalDateTime.now LocalDateTime = ();
return localDateTime.plusMonths (months);
}

/ **
* Modified current time in days
* @param days day
* @return
* /
public static. plusDays the LocalDateTime (int days) {
the LocalDateTime LocalDateTime.now LocalDateTime = ();
return localDateTime.plusDays (days);
}

/ **
* subtraction current time week
* @param weeks weeks
* @return
* /
public static plusWeeks the LocalDateTime (int weeks) {
the LocalDateTime LocalDateTime.now LocalDateTime = ();
localDateTime.plusWeeks return (weeks);
}

/ **
* current time plus a few hours, by subtracting a time corresponding to the incoming negative
* @param hours time: in hours
* @return the LocalDateTime
* /
public static plusHours the LocalDateTime (int hours ) {
the LocalDateTime LocalDateTime.now LocalDateTime = ();
return localDateTime.plusHours (hours);
}

/ **
* the current time plus a few minutes, by subtracting the time corresponding to the incoming negative
* @param minutes time: in minutes
* @return the LocalDateTime
* /
public static plusMinutes the LocalDateTime (int minutes) {
the LocalDateTime LocalDateTime.now LocalDateTime = ();
return localDateTime.plusMinutes (minutes);
}

/ **
* the current time plus a few seconds, corresponding to an incoming time by subtracting the negative
* @param seconds time: in seconds
@Return the LocalDateTime *
* /
public static plusSeconds the LocalDateTime (seconds The int) {
the LocalDateTime LocalDateTime.now LocalDateTime = ();
return localDateTime.plusSeconds (seconds The);
}

/ **
* Modify hours of the current time
* @param hour time: in hours
@return the LocalDateTime *
* /
public static withHour the LocalDateTime (int hour) {
the LocalDateTime LocalDateTime.now LocalDateTime = ();
return localDateTime.withHour (hour);
}

/ **
* modify the current time minutes
* @param minute time: in minutes
@return the LocalDateTime *
* /
public static withMinute the LocalDateTime (int minute) {
the LocalDateTime LocalDateTime.now LocalDateTime = ();
localDateTime.withMinute return (minute);
}

/ **
* Modify the current time in seconds
* @param second time: in seconds
* @return the LocalDateTime
* /
public static withSecond the LocalDateTime (SECOND int) {
the LocalDateTime LocalDateTime.now LocalDateTime = ();
localDateTime.withSecond return (SECOND,);
}

** /
* add years to the time specified
data specified period of time * @param
year negative * @param years to add to subtract
* @return the LocalDateTime
* /
public static the LocalDateTime addYears (String Data, int years) {
the LocalDateTime LocalDateTime = formatStringToDate (Data);
return localDateTime.plusYears (years);
}

/ **
* to the designated time plus month
* @param data specified time
* @Param months of May to add the negative of subtracting
* @return the LocalDateTime
* /
public static addMonths the LocalDateTime (Data String, int months) {
the LocalDateTime LocalDateTime = formatStringToDate (Data);
return localDateTime.plusMonths (months);
}

/ * *
* time to add a specified date
* @param data specified time
* @param days to add the negative of subtracting day
* @return the LocalDateTime
* /
public static addDays the LocalDateTime (Data String, int days) {
the LocalDateTime formatStringToDate LocalDateTime = ( Data);
return localDateTime.plusDays (Days);
}

/ **
* to the designated time plus week
* @param data specified time
* @param weeks to be added by subtracting negative week
* @return the LocalDateTime
* /
the LocalDateTime addWeeks static public (Data String, int weeks) {
the LocalDateTime LocalDateTime = formatStringToDate (Data);
return localDateTime.plusWeeks (weeks);
}

/ **
* Hour applied to the specified time
* @param data specified time
* @param hour to add h subtracting the negative
* @return the LocalDateTime
* /
public static addHours the LocalDateTime (Data String, int hour) {
the LocalDateTime LocalDateTime = formatStringToDate (Data);
return localDateTime.plusHours (hour);
}

/ **
* to the designated time plus min
* @param data specified time
* @param minutes to be added by subtracting negative minutes
* @return the LocalDateTime
* /
public static AddMinutes the LocalDateTime (Data String, int minutes) {
LocalDateTime = formatStringToDate the LocalDateTime (Data);
return localDateTime.plusMinutes (minutes);
}

/ **
* applied to the specified time in seconds
* @param data specified time
* @param seconds to add the negative of the second subtracting
* @return the LocalDateTime
* /
public static AddSeconds the LocalDateTime (String Data, seconds The int) {
the LocalDateTime LocalDateTime = formatStringToDate (Data);
return localDateTime.plusSeconds (seconds The);
}

Guess you like

Origin www.cnblogs.com/wangjinyu/p/11494225.html