Java編程時(shí)間日期API實(shí)例解析
本文實(shí)例主要是關(guān)于Java8中的新特性,時(shí)間日期api的相關(guān)實(shí)例,具體如下:
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è)置小時(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);
}
/**
* 修改時(shí)分秒
* @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;
}
/**
* 日期時(shí)分秒加
* @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;
}
/**
* 時(shí)分秒加減
* @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);
}
代碼涉及時(shí)間日期類的使用等內(nèi)容,具有簡(jiǎn)單注釋,大家可自行參考。
總結(jié)
以上就是本文關(guān)于Java編程時(shí)間日期API實(shí)例解析的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Spring Boot學(xué)習(xí)入門之表單驗(yàn)證
表單驗(yàn)證主要是用來(lái)防范小白搞亂網(wǎng)站和一些低級(jí)的黑客技術(shù)。Spring Boot可以使用注解 @Valid 進(jìn)行表單驗(yàn)證。下面這篇文章主要給大家介紹了關(guān)于Spring Boot學(xué)習(xí)入門之表單驗(yàn)證的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09
spring-boot-starter-security的簡(jiǎn)單使用方式
文章介紹了三種使用Spring Boot Security的方法:基于配置文件、基于配置類和基于注解的方式,通過(guò)這些方法,可以實(shí)現(xiàn)對(duì)Web應(yīng)用的權(quán)限控制,確保只有授權(quán)用戶才能訪問(wèn)特定資源2024-11-11
SpringMVC表單標(biāo)簽知識(shí)點(diǎn)詳解
這篇文章主要為大家詳細(xì)介紹了SpringMVC表單標(biāo)簽知識(shí)點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
java設(shè)計(jì)模式之橋接模式(Bridge)
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之橋接模式Bridge,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
SpringCloud?分布式微服務(wù)架構(gòu)操作步驟
SpringCloud是一種微服務(wù)的框架,利用它我們可以去做分布式服務(wù)開(kāi)發(fā),這篇文章主要介紹了SpringCloud?分布式微服務(wù)架構(gòu),需要的朋友可以參考下2022-07-07
Java配置JDK開(kāi)發(fā)環(huán)境及環(huán)境變量
這篇文章主要為大家詳細(xì)介紹了Java配置JDK開(kāi)發(fā)環(huán)境及環(huán)境變量,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
JAVA實(shí)現(xiàn)用戶抽獎(jiǎng)功能(附完整代碼)
這篇文章主要給大家介紹了關(guān)于JAVA實(shí)現(xiàn)用戶抽獎(jiǎng)功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

