Java8中的LocalDateTime和Date一些時(shí)間操作方法
先記錄下jdk8之前的一些幫助方法
判斷time是否在now的n天之內(nèi)
/**
* 判斷time是否在now的n天之內(nèi)
* @param time
* @param now
* @param n 正數(shù)表示在條件時(shí)間n天之后,負(fù)數(shù)表示在條件時(shí)間n天之前
* @return
*/
public static boolean belongDate(Date time, Date now, int n) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance(); //得到日歷
calendar.setTime(now);//把當(dāng)前時(shí)間賦給日歷
calendar.add(Calendar.DAY_OF_MONTH, n);
Date before7days = calendar.getTime(); //得到n前的時(shí)間
if (before7days.getTime() < time.getTime()) {
return true;
} else {
return false;
}
}
判斷某個(gè)時(shí)間是否是在條件的起始時(shí)間與結(jié)束時(shí)間之內(nèi)
/**
* 判斷time是否在from,to之內(nèi)
*
* @param time 指定日期
* @param from 開(kāi)始日期
* @param to 結(jié)束日期
* @return
*/
public static boolean belongCalendar(Date time, Date from, Date to) {
Calendar date = Calendar.getInstance();
date.setTime(time);
Calendar after = Calendar.getInstance();
after.setTime(from);
Calendar before = Calendar.getInstance();
before.setTime(to);
if (date.after(after) && date.before(before)) {
return true;
} else {
return false;
}
}
判斷給定時(shí)間與當(dāng)前時(shí)間相差多少天
public static long getDistanceDays(String date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
long days = 0;
try {
Date time = df.parse(date);//String轉(zhuǎn)Date
Date now = new Date();//獲取當(dāng)前時(shí)間
long time1 = time.getTime();
long time2 = now.getTime();
long diff = time1 - time2;
days = diff / (1000 * 60 * 60 * 24);
} catch (Exception e) {
e.printStackTrace();
}
return days;//正數(shù)表示在當(dāng)前時(shí)間之后,負(fù)數(shù)表示在當(dāng)前時(shí)間之前
}
將Date轉(zhuǎn)換成String
private static final String LONG_PATTERN="yyyy-MM-dd HH:mm:ss";
private static final String SHORT_PATTERN="yyyy-MM-dd";
/**
* 將日期轉(zhuǎn)換為字符串
*/
public static String parse( Date d, String pattern){
DateFormat df=null;
if( pattern!=null && !"".equals(pattern) ){
df=new SimpleDateFormat(pattern);
}else{
df=new SimpleDateFormat(LONG_PATTERN);
}
return df.format( d );
}
將String轉(zhuǎn)換成Date
private static final String LONG_PATTERN="yyyy-MM-dd HH:mm:ss";
private static final String SHORT_PATTERN="yyyy-MM-dd";
/**
* 將字符串轉(zhuǎn)為日期
*/
public static Date parseStringToDate(String str, String pattern){
DateFormat df = null;
if( pattern!=null && !"".equals(pattern) ){
df=new SimpleDateFormat( pattern );
}else{
df=new SimpleDateFormat( LONG_PATTERN );
}
Date d=null;
try {
d=df.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
獲取指定年后的日期(例如三年后的日期)
Calendar date = Calendar.getInstance();
date.setTime(new Date());
date.add(Calendar.YEAR, +3);
//倒計(jì)時(shí)結(jié)束后的時(shí)間
Date scrap_year = date.getTime();
System.out.println("三年后時(shí)間" + scrap_year);
Jdk8改革
LocalDateTime獲取毫秒數(shù)
//獲取秒數(shù)
Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
//獲取毫秒數(shù)
Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
LocalDateTime與String互轉(zhuǎn)
//時(shí)間轉(zhuǎn)字符串格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
//字符串轉(zhuǎn)時(shí)間
String dateTimeStr = "2018-07-28 14:11:15";
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, df);
Date與LocalDateTime互轉(zhuǎn)
//將java.util.Date 轉(zhuǎn)換為java8 的java.time.LocalDateTime,默認(rèn)時(shí)區(qū)為東8區(qū)
public static LocalDateTime dateConvertToLocalDateTime(Date date) {
return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
}
//將java8 的 java.time.LocalDateTime 轉(zhuǎn)換為 java.util.Date,默認(rèn)時(shí)區(qū)為東8區(qū)
public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
}
將LocalDateTime轉(zhuǎn)為自定義的時(shí)間格式的字符串
public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
return localDateTime.format(formatter);
}
將某時(shí)間字符串轉(zhuǎn)為自定義時(shí)間格式的LocalDateTime
public static LocalDateTime parseStringToDateTime(String time, String format) {
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
return LocalDateTime.parse(time, df);
}
將long類(lèi)型的timestamp轉(zhuǎn)為L(zhǎng)ocalDateTime
public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
Instant instant = Instant.ofEpochMilli(timestamp);
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
將LocalDateTime轉(zhuǎn)為long類(lèi)型的timestamp
public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return instant.toEpochMilli();
}
總結(jié)
到此這篇關(guān)于Java8中的LocalDateTime和Date一些時(shí)間操作方法的文章就介紹到這了,更多相關(guān)java8 localdateTime和date內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java8時(shí)間轉(zhuǎn)換(LocalDateTime)代碼實(shí)例
- Java如何利用LocalDate獲取某個(gè)月的第一天與最后一天日期
- java用LocalDateTime類(lèi)獲取當(dāng)天時(shí)間、前一天時(shí)間及本周/本月的開(kāi)始和結(jié)束時(shí)間
- Java日期工具類(lèi)操作字符串Date和LocalDate互轉(zhuǎn)
- java8之LocalDate的使用、LocalDate格式化問(wèn)題
- Java中各類(lèi)日期和時(shí)間轉(zhuǎn)換超詳析總結(jié)(Date和LocalDateTime相互轉(zhuǎn)換等)
相關(guān)文章
Java實(shí)現(xiàn)PDF文件的分割與加密功能
這篇文章主要為大家分享了如何利用Java語(yǔ)言實(shí)現(xiàn)PDF文件的分割與加密以及封面圖的生成,文中的示例代碼簡(jiǎn)潔易懂,感興趣的可以了解一下2022-04-04
Spring?Boot如何接入Security權(quán)限認(rèn)證服務(wù)
Spring Security?是一個(gè)高度可定制的身份驗(yàn)證和訪問(wèn)控制的框架,提供了完善的認(rèn)證機(jī)制和方法級(jí)的授權(quán)功能,本文通過(guò)案例將Spring Security整合到SpringBoot中,要實(shí)現(xiàn)的功能就是在認(rèn)證服務(wù)器上登錄,然后獲取Token,再訪問(wèn)資源服務(wù)器中的資源,感興趣的朋友一起看看吧2024-07-07
java使用HashMap實(shí)現(xiàn)斗地主(有序版)
這篇文章主要為大家詳細(xì)介紹了java使用ArrayList實(shí)現(xiàn)斗地主游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
JavaWeb利用struts實(shí)現(xiàn)文件下載時(shí)改變文件名稱(chēng)
這篇文章主要為大家詳細(xì)介紹了JavaWeb利用struts實(shí)現(xiàn)文件下載時(shí)改變文件名稱(chēng)的相關(guān)資料,需要的朋友可以參考下2016-06-06
Maven?項(xiàng)目用Assembly打包可執(zhí)行jar包的方法
這篇文章主要介紹了Maven?項(xiàng)目用Assembly打包可執(zhí)行jar包的方法,該方法只可打包非spring項(xiàng)目的可執(zhí)行jar包,需要的朋友可以參考下2023-03-03
java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07

