The examples in this article are mainly about new features in Java 8 and related examples of time and date API, as follows:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Date tool class * @author yanweiqi * @since 2016-5-6 * */ public class LocalDateUtils { private static ZoneId zone = (); /** * String to Date * @param date * @return * @throws Exception */ public static Date convertToDate(String date) throws Exception{ LocalDate localDate = null; if(null == date){ throw new NullPointerException("date isn't null"); } else { localDate = (date); return convertToDate(localDate); } } /** * String to LocalDateTime * @param date * @return localDateTime */ public static LocalDateTime convertToLocalDateTime(String date){ LocalDateTime localDateTime = null; if(null == date){ throw new NullPointerException("date isn't null"); } else { localDateTime = (date); return localDateTime; } } /** * LocalDate to Date * @param localDate * @return Date */ public static Date convertToDate(LocalDate localDate){ Instant instant = ().atZone(zone).toInstant(); return (instant); } /** * LocalDate to Date * @param localDateTime * @return Date */ public static Date convertToDate(LocalDateTime localDateTime){ Instant instant = (zone).toInstant(); return (instant); } /** * Date to LocalDate * @param date * @return localDate */ public static LocalDate convertToLocalDate(Date date){ Instant instant = (); return convertToLocalDateTime(instant).toLocalDate(); } /** * Date to LocalTime * @param date * @return localDate */ public static LocalTime convertToLocalTime(Date date){ Instant instant = (); return convertToLocalDateTime(instant).toLocalTime(); } /** * Date to LocalDatetime * @param date * @return localDate */ public static LocalDateTime convertToLocalDateTime(Date date){ Instant instant = (); return convertToLocalDateTime(instant); } /** * Instant to LocalDateTime * @param instant * @return */ public static LocalDateTime convertToLocalDateTime(Instant instant){ return (instant, zone); } /** * LocalDateTime to Instant * @param localDateTime * @return */ public static Instant convertToInstant(LocalDateTime localDateTime){ return (zone).toInstant(); } /** * LocalDate to Instant * @param localDate * @return */ public static Instant convertToInstant(LocalDate localDate){ return (zone).toInstant(); } /** * LocalDate to LocalDateTime * @param localDate * @return LocalDateTime */ public static LocalDateTime convertToLocalDateTime(LocalDate localDate){ return (); } /** *Daily cycle formatting * @param localDateTime * @param formatStyle * @return */ public static String formatter(LocalDateTime localDateTime, String formatStyle){ return (formatStyle).format(localDateTime); } /** * Set year * @param sourceDate * @param year * @return LocalDateTime */ public static LocalDateTime setYear(LocalDateTime sourceDate, Integer year){ return (year); } /** * Set the month * @param sourceDate * @param month * @return LocalDateTime */ public static LocalDateTime setMonth(LocalDateTime sourceDate, Integer month){ return (month); } /** * Set the day * @param sourceDate * @param month * @return LocalDateTime */ public static LocalDateTime setDayOfMonth(LocalDateTime sourceDate, Integer dayOfMonth){ return (dayOfMonth); } /** * Set hours * @param sourceDate * @param hour * @return */ public static LocalDateTime setHour(LocalDateTime sourceDate,Integer hour){ return (hour); } /** * Set minutes * @param sourceDate * @param minute * @return */ public static LocalDateTime setMinute(LocalDateTime sourceDate,Integer minute){ return (minute); } /** * Set seconds * @param sourceDate * @param second * @return */ public static LocalDateTime setSecond(LocalDateTime sourceDate,Integer second){ return (second); } /** * Modify the year, month, date * @param sourceDate * @param year * @param month * @param dayOfMonth * @return */ public static LocalDateTime setYMD(LocalDateTime sourceDate, Integer year, Integer month, Integer dayOfMonth) { return (year).withMonth(month).withDayOfMonth(dayOfMonth); } /** * Modify time, minute and second * @param sourceDate * @param hour * @param minute * @param second * @return */ public static LocalDateTime setHMS(LocalDateTime sourceDate,Integer hour, Integer minute, Integer second) { return (hour).withMinute(minute).withSecond(second); } /** * Calculate the number of days of difference * @param beginDate * @param endDate * @return */ public static int getInteverDays(LocalDate beginDate,LocalDate endDate){ Period period = (beginDate, endDate); return (); } /** * Date addition and subtraction * @param num quantity * @param unit * @param LocalDate Original date * @return LocalDate The date after the increase */ @SuppressWarnings("static-access") public static LocalDate addLocalDate(long num,ChronoUnit unit,final LocalDate localDate){ LocalDate resultDate; if(num > 0){ resultDate = ().plus(num, unit); } else { resultDate = ().minus((num), unit); } return resultDate; } /** * Date, hour, minute and second * @param num quantity * @param unit * @param localDateTime Original date * @return LocalDateTime The date after the increase */ @SuppressWarnings("static-access") public static LocalDateTime addLocalDateTime(long num,ChronoUnit unit,LocalDateTime localDateTime){ LocalDateTime resultDateTime; if(num > 0){ resultDateTime = ().plus(num, unit); } else { resultDateTime = ().minus((num),unit); } return resultDateTime; } /** * Add or subtract from hours, minutes and seconds * @param num quantity * @param unit * @param localTime Original date * @return LocalDateTime The date after the increase */ @SuppressWarnings("static-access") public static LocalTime addLocalTime(long num,ChronoUnit unit,LocalTime localTime){ LocalTime resultTime; if(num > 0){ resultTime = ().plus(num, unit); } else { resultTime = ().minus((num), unit); } return resultTime; } public static void main(String[] args){ LocalDateTime time = (); String rr = formatter(time, "yyyy-MM-dd HH:mm:ss"); (rr); LocalDateTime time2 = addLocalDateTime(-2, , time); String yy = formatter(time2, "yyyy-MM-dd HH:mm:ss"); (yy); }
The code involves the use of time and date classes, and has simple comments, which can be referenced by you.
Summarize
The above is all the content of this article about the analysis of Java programming time and date API instances, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!