Java編程時間日期API執行個體解析,java編程api執行個體

來源:互聯網
上載者:User

Java編程時間日期API執行個體解析,java編程api執行個體

本文執行個體主要是關於Java8中的新特性,時間日期api的相關執行個體,具體如下:

package com.effective.common.base.date;import java.time.Instant;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.LocalTime;import java.time.Period;import java.time.ZoneId;import java.time.format.DateTimeFormatter;import java.time.temporal.ChronoUnit;import java.util.Date;/** * 日期工具類 * @author yanweiqi * @since 2016-5-6 * */public class LocalDateUtils {private static ZoneId zone = ZoneId.systemDefault();/**   * 字串轉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 = LocalDate.parse(date);return convertToDate(localDate);}}/**   * 字串轉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 = LocalDateTime.parse(date);return localDateTime;}}/**   * LocalDate轉Date   * @param localDate   * @return Date   */public static Date convertToDate(LocalDate localDate){Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();return Date.from(instant);}/**   * LocalDate轉Date   * @param localDateTime   * @return Date   */public static Date convertToDate(LocalDateTime localDateTime){Instant instant = localDateTime.atZone(zone).toInstant();return Date.from(instant);}/**   * Date轉LocalDate   * @param date   * @return localDate   */public static LocalDate convertToLocalDate(Date date){Instant instant = date.toInstant();return convertToLocalDateTime(instant).toLocalDate();}/**   * Date轉LocalTime   * @param date   * @return localDate   */public static LocalTime convertToLocalTime(Date date){Instant instant = date.toInstant();return convertToLocalDateTime(instant).toLocalTime();}/**   * Date轉LocalDatetime   * @param date   * @return localDate   */public static LocalDateTime convertToLocalDateTime(Date date){Instant instant = date.toInstant();return convertToLocalDateTime(instant);}/**   * Instant轉LocalDateTime   * @param instant   * @return   */public static LocalDateTime convertToLocalDateTime(Instant instant){return LocalDateTime.ofInstant(instant, zone);}/**   * LocalDateTime轉Instant   * @param localDateTime   * @return   */public static Instant convertToInstant(LocalDateTime localDateTime){return localDateTime.atZone(zone).toInstant();}/**   * LocalDate轉Instant   * @param localDate   * @return   */public static Instant convertToInstant(LocalDate localDate){return localDate.atStartOfDay(zone).toInstant();}/**   * LocalDate轉LocalDateTime   * @param localDate   * @return LocalDateTime   */public static LocalDateTime convertToLocalDateTime(LocalDate localDate){return localDate.atStartOfDay();}/**   * 日周期格式化   * @param localDateTime   * @param formatStyle   * @return   */public static String formatter(LocalDateTime localDateTime, String formatStyle){return DateTimeFormatter.ofPattern(formatStyle).format(localDateTime);}/**   * 設定年   * @param sourceDate   * @param year   * @return LocalDateTime   */public static LocalDateTime setYear(LocalDateTime sourceDate, Integer year){return sourceDate.withYear(year);}/**   * 設定月   * @param sourceDate   * @param month   * @return LocalDateTime   */public static LocalDateTime setMonth(LocalDateTime sourceDate, Integer month){return sourceDate.withMonth(month);}/**   * 設定天   * @param sourceDate   * @param month   * @return LocalDateTime   */public static LocalDateTime setDayOfMonth(LocalDateTime sourceDate, Integer dayOfMonth){return sourceDate.withDayOfMonth(dayOfMonth);}/**   * 設定小時   * @param sourceDate   * @param hour   * @return   */public static LocalDateTime setHour(LocalDateTime sourceDate,Integer hour){return sourceDate.withHour(hour);}/**   * 設定分鐘   * @param sourceDate   * @param minute   * @return   */public static LocalDateTime setMinute(LocalDateTime sourceDate,Integer minute){return sourceDate.withMinute(minute);}/**   * 設定秒   * @param sourceDate   * @param second   * @return   */public static LocalDateTime setSecond(LocalDateTime sourceDate,Integer second){return sourceDate.withSecond(second);}/**   * 修改年月日   * @param sourceDate   * @param year   * @param month   * @param dayOfMonth   * @return   */public static LocalDateTime setYMD(LocalDateTime sourceDate, Integer year, Integer month, Integer dayOfMonth) {return sourceDate.withYear(year).withMonth(month).withDayOfMonth(dayOfMonth);}/**   * 修改時分秒   * @param sourceDate   * @param hour   * @param minute   * @param second   * @return   */public static LocalDateTime setHMS(LocalDateTime sourceDate,Integer hour, Integer minute, Integer second) {return sourceDate.withHour(hour).withMinute(minute).withSecond(second);}/**   * 計算相差的天數   * @param beginDate   * @param endDate   * @return   */public static int getInteverDays(LocalDate beginDate,LocalDate endDate){Period period = Period.between(beginDate, endDate);return period.getDays();}/**   * 日期加減   * @param num 數量   * @param unit 單位   * @param LocalDate 原日期   * @return LocalDate 增加後的日期   */@SuppressWarnings("static-access")  public static LocalDate addLocalDate(long num,ChronoUnit unit,final LocalDate localDate){LocalDate resultDate;if(num > 0){resultDate = localDate.now().plus(num, unit);} else {resultDate = localDate.now().minus(Math.abs(num), unit);}return resultDate;}/**   * 日期時分秒加   * @param num 數量   * @param unit 單位   * @param localDateTime 原日期   * @return LocalDateTime 增加後的日期   */@SuppressWarnings("static-access")  public static LocalDateTime addLocalDateTime(long num,ChronoUnit unit,LocalDateTime localDateTime){LocalDateTime resultDateTime;if(num > 0){resultDateTime = localDateTime.now().plus(num, unit);} else {resultDateTime = localDateTime.now().minus(Math.abs(num),unit);}return resultDateTime;}/**   * 時分秒加減   * @param num 數量   * @param unit 單位   * @param localTime 原日期   * @return LocalDateTime 增加後的日期   */@SuppressWarnings("static-access")  public static LocalTime addLocalTime(long num,ChronoUnit unit,LocalTime localTime){LocalTime resultTime;if(num > 0){resultTime = localTime.now().plus(num, unit);} else {resultTime = localTime.now().minus(Math.abs(num), unit);}return resultTime;}public static void main(String[] args){LocalDateTime time = LocalDateTime.now();String rr = formatter(time, "yyyy-MM-dd HH:mm:ss");System.out.println(rr);LocalDateTime time2 = addLocalDateTime(-2, ChronoUnit.HOURS, time);String yy = formatter(time2, "yyyy-MM-dd HH:mm:ss");System.out.println(yy);}

代碼涉及時間日期類的使用等內容,具有簡單注釋,大家可自行參考。

總結

以上就是本文關於Java編程時間日期API執行個體解析的全部內容,希望對大家有所協助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.