小編給大家分享一下Java編程中時間日期API的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都服務(wù)器托管,創(chuàng)新互聯(lián)建站提供包括服務(wù)器租用、西云機(jī)房、帶寬租用、云主機(jī)、機(jī)柜租用、主機(jī)租用托管、CDN網(wǎng)站加速、域名申請等業(yè)務(wù)的一體化完整服務(wù)。電話咨詢:028-86922220
具體如下:
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(); /** * 字符串轉(zhuǎn)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); } } /** * 字符串轉(zhuǎn)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轉(zhuǎn)Date * @param localDate * @return Date */ public static Date convertToDate(LocalDate localDate){ Instant instant = localDate.atStartOfDay().atZone(zone).toInstant(); return Date.from(instant); } /** * LocalDate轉(zhuǎn)Date * @param localDateTime * @return Date */ public static Date convertToDate(LocalDateTime localDateTime){ Instant instant = localDateTime.atZone(zone).toInstant(); return Date.from(instant); } /** * Date轉(zhuǎn)LocalDate * @param date * @return localDate */ public static LocalDate convertToLocalDate(Date date){ Instant instant = date.toInstant(); return convertToLocalDateTime(instant).toLocalDate(); } /** * Date轉(zhuǎn)LocalTime * @param date * @return localDate */ public static LocalTime convertToLocalTime(Date date){ Instant instant = date.toInstant(); return convertToLocalDateTime(instant).toLocalTime(); } /** * Date轉(zhuǎn)LocalDatetime * @param date * @return localDate */ public static LocalDateTime convertToLocalDateTime(Date date){ Instant instant = date.toInstant(); return convertToLocalDateTime(instant); } /** * Instant轉(zhuǎn)LocalDateTime * @param instant * @return */ public static LocalDateTime convertToLocalDateTime(Instant instant){ return LocalDateTime.ofInstant(instant, zone); } /** * LocalDateTime轉(zhuǎn)Instant * @param localDateTime * @return */ public static Instant convertToInstant(LocalDateTime localDateTime){ return localDateTime.atZone(zone).toInstant(); } /** * LocalDate轉(zhuǎn)Instant * @param localDate * @return */ public static Instant convertToInstant(LocalDate localDate){ return localDate.atStartOfDay(zone).toInstant(); } /** * LocalDate轉(zhuǎn)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); } /** * 設(shè)置年 * @param sourceDate * @param year * @return LocalDateTime */ public static LocalDateTime setYear(LocalDateTime sourceDate, Integer year){ return sourceDate.withYear(year); } /** * 設(shè)置月 * @param sourceDate * @param month * @return LocalDateTime */ public static LocalDateTime setMonth(LocalDateTime sourceDate, Integer month){ return sourceDate.withMonth(month); } /** * 設(shè)置天 * @param sourceDate * @param month * @return LocalDateTime */ public static LocalDateTime setDayOfMonth(LocalDateTime sourceDate, Integer dayOfMonth){ return sourceDate.withDayOfMonth(dayOfMonth); } /** * 設(shè)置小時 * @param sourceDate * @param hour * @return */ public static LocalDateTime setHour(LocalDateTime sourceDate,Integer hour){ return sourceDate.withHour(hour); } /** * 設(shè)置分鐘 * @param sourceDate * @param minute * @return */ public static LocalDateTime setMinute(LocalDateTime sourceDate,Integer minute){ return sourceDate.withMinute(minute); } /** * 設(shè)置秒 * @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); } /** * 計(jì)算相差的天數(shù) * @param beginDate * @param endDate * @return */ public static int getInteverDays(LocalDate beginDate,LocalDate endDate){ Period period = Period.between(beginDate, endDate); return period.getDays(); } /** * 日期加減 * @param num 數(shù)量 * @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 數(shù)量 * @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 數(shù)量 * @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的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁名稱:Java編程中時間日期API的示例分析
URL網(wǎng)址:http://www.muchs.cn/article48/piejhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、品牌網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)公司、動態(tài)網(wǎng)站、標(biāo)簽優(yōu)化、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)