## LocalDate、LocalDateTime與timestamp、Date的轉換
#### 1.LocalDate轉Date
```
LocalDate nowLocalDate = LocalDate.now();
Date date = Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
```
#### 2.LocalDateTime轉Date
```
LocalDateTime localDateTime = LocalDateTime.now();
Date date = Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());
```
#### 3.Date轉LocalDateTime(LocalDate)
```
Date date = new Date();
LocalDateTime localDateTime = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();
```
#### 4.LocalDate轉時間戳
```
LocalDate localDate = LocalDate.now();
long timestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
```
#### 5.LocalDateTime轉時間戳
```
LocalDateTime localDateTime = LocalDateTime.now();
long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
```
#### 6.時間戳轉LocalDateTime(LocalDate)
```
long timestamp = System.currentTimeMillis();
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
```
#### 7.工具類
```
import com.alibaba.druid.util.StringUtils;
import org.springframework.util.Assert;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Calendar;
import java.util.Date;
/**
* jdk1.8使用
* 日期時間工具類
*/
public class DateTimeUtils {
public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static final String DATE_FORMATTER_CON = "yyyyMMdd";
public static final long tenSecond= 30 * 1000;
/**
* Date轉換為LocalDateTime
*
* @param date
* @return
*/
public static LocalDateTime convertToLocalDateTime(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
/**
* local時間轉換成UTC時間
* @param currentTimeStamp
* @return
*/
public static Date localToUTC(Long currentTimeStamp) {
String localTime = DateTimeUtils.convertTimeToString(currentTimeStamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date localDate= null;
try {
localDate = sdf.parse(localTime);
} catch (ParseException e) {
e.printStackTrace();
}
long localTimeInMillis=localDate.getTime();
/** long時間轉換成Calendar */
Calendar calendar= Calendar.getInstance();
calendar.setTimeInMillis(localTimeInMillis);
/** 取得時間偏移量 */
int zoneOffset = calendar.get(Calendar.ZONE_OFFSET);
/** 取得夏令時差 */
int dstOffset = calendar.get(Calendar.DST_OFFSET);
/** 從本地時間里扣除這些差量,即可以取得UTC時間*/
calendar.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
/** 取得的時間就是UTC標準時間 */
Date utcDate=new Date(calendar.getTimeInMillis());
return utcDate;
}
/**
* LocalDateTime轉換為Date
*
* @param dateTime
* @return
*/
public static Date convertToDate(LocalDateTime dateTime) {
return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* 返回本地當前日期
*
* @return
*/
public static LocalDate getCurrentLocalDate() {
return LocalDate.now();
}
/**
* 返回本地當前時間
*
* @return
*/
public static LocalTime getCurrentLocalTime() {
return LocalTime.now();
}
/**
* 返回本地當前日期時間
*
* @return
*/
public static LocalDateTime getCurrentLocalDateTime() {
return LocalDateTime.now();
}
/**
* 返回本地當前日期的固定或自定義字符串
*
* @return
*/
public static String getCurrentDateStr(String pattern) {
if (StringUtils.isEmpty(pattern)) {
return LocalDate.now().format(DateTimeFormatter.ofPattern(pattern));
}
return LocalDate.now().format(DATE_FORMATTER);
}
/**
* 返回本地當前時間的固定或自定義字符串
*
* @return
*/
public static String getCurrentTimeStr(String pattern) {
if (StringUtils.isEmpty(pattern)) {
return LocalTime.now().format(DateTimeFormatter.ofPattern(pattern));
}
return LocalTime.now().format(TIME_FORMATTER);
}
/**
* 返回本地當前日期時間的固定或自定義字符串
*
* @return
*/
public static String getCurrentDateTimeStr(String pattern) {
if (StringUtils.isEmpty(pattern)) {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern));
}
return LocalDateTime.now().format(DATETIME_FORMATTER);
}
/**
* 將本地日期字符串根據pattern解析出本地日期
*
* @param dateStr
* @param pattern
* @return
*/
public static LocalDate parseLocalDate(String dateStr, String pattern) {
return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
}
/**
* 將本地時間字符串根據pattern解析出本地時間
*
* @param timeStr
* @param pattern
* @return
*/
public static LocalTime parseLocalTime(String timeStr, String pattern) {
return LocalTime.parse(timeStr, DateTimeFormatter.ofPattern(pattern));
}
/**
* 將本地日期字符串根據pattern解析出本地日期
*
* @param dateTimeStr
* @param pattern
* @return
*/
public static LocalDateTime parseLocalDateTime(String dateTimeStr, String pattern) {
return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(pattern));
}
/**
* 將本地日期轉化為固定或自定義格式的字符串
*
* @param date
* @return
*/
public static String formatLocalDate(LocalDate date, String pattern) {
if (StringUtils.isEmpty(pattern)) {
return date.format(DateTimeFormatter.ofPattern(pattern));
}
return date.format(DATE_FORMATTER);
}
/**
* 將本地時間轉化為固定或自定義格式的字符串
*
* @param time
* @return
*/
public static String formatLocalTime(LocalTime time, String pattern) {
if (StringUtils.isEmpty(pattern)) {
return time.format(DateTimeFormatter.ofPattern(pattern));
}
return time.format(TIME_FORMATTER);
}
/**
* 將本地日期時間轉化為固定或自定義格式的字符串
*
* @param datetime
* @return
*/
public static String formatLocalDateTime(LocalDateTime datetime, String pattern) {
if (StringUtils.isEmpty(pattern)) {
return datetime.format(DateTimeFormatter.ofPattern(pattern));
}
return datetime.format(DATETIME_FORMATTER);
}
/**
* 獲取指定日期的秒
*
* @param dateTime
* @return
*/
public static Long getSeconds(LocalDateTime dateTime) {
return dateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
}
/**
* 獲取指定日期的毫秒(轉時間戳)
* @param dateTime
* @return
*/
public static Long getMillis(LocalDateTime dateTime) {
return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* 字符串轉轉時間戳
* @param time
* @return
*/
public static Long strToLongGetMillis(String time) {
return LocalDateTime.parse(time, DATETIME_FORMATTER).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* 時間戳轉LocalDateTime
*
* @param timestamp
* @return
*/
public static LocalDateTime timestampToLDT(Long timestamp) {
return LocalDateTime.ofEpochSecond(timestamp / 1000, (int) (timestamp % 1000) * 1000000, ZoneOffset.ofHours(8));
}
/**
* 本地日期時間加上一個數
*
* @param dateTime
* @param num
* @param field ChronoUnit.*(年、月、周、日、分...)
* @return
*/
public static LocalDateTime plus(LocalDateTime dateTime, long num, TemporalUnit field) {
return dateTime.plus(num, field);
}
/**
* 本地日期時間減去一個數
*
* @param dateTime
* @param num
* @param field ChronoUnit.*(年、月、周、日、分...)
* @return
*/
public static LocalDateTime minu(LocalDateTime dateTime, long num, TemporalUnit field) {
return dateTime.minus(num, field);
}
/**
*本地日期時間減去一個數 Date時間格式
* @param dateTime
* @param num
* @param field ChronoUnit.*(年、月、周、日、分...)
* @return
*/
public static Date getMinu(Date dateTime, long num, TemporalUnit field) {
LocalDateTime localDateTime = DateTimeUtils.minu(DateTimeUtils.convertToLocalDateTime(dateTime),
num, field);
return DateTimeUtils.convertToDate(localDateTime);
}
/**
*本地日期時間加一個數 Date時間格式
* @param dateTime
* @param num
* @param field ChronoUnit.*(年、月、周、日、分...)
* @return
*/
public static Date getPlus(Date dateTime, long num, TemporalUnit field) {
LocalDateTime localDateTime = DateTimeUtils.plus(DateTimeUtils.convertToLocalDateTime(dateTime),
num, field);
return DateTimeUtils.convertToDate(localDateTime);
}
/**
* 獲取本地兩個日期的時間差
*
* @param startTime
* @param endTime
* @param field
* @return
*/
public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, TemporalUnit field) {
Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
if (field == ChronoUnit.YEARS) {
return period.getYears();
} else if (field == ChronoUnit.MONTHS) {
return period.getYears() * 12 + period.getMonths();
}
return field.between(startTime, endTime);
}
/**
* 根據LocalDateTime獲取一天的開始時間
*
* @param dateTime
* @return
*/
public static LocalDateTime getDateStart(LocalDateTime dateTime) {
return dateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
}
/**
* 根據LocalDateTime獲取一天的結束時間
*
* @param dateTime
* @return
*/
public static LocalDateTime getDateEnd(LocalDateTime dateTime) {
return dateTime.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
}
/**
* 根據LocalDate獲取一天的結束時間
*
* @param date
* @return
*/
public static LocalDateTime getLocalDateEnd(LocalDate date) {
return date.atTime(23, 59, 59, 999999999);
}
/**
* 根據LocalDate獲取一天的開始時間
*
* @param date
* @return
*/
public static LocalDateTime getLocalDateStart(LocalDate date) {
return date.atTime(0, 0, 0, 0);
}
/**
* 是否當天
*
* @param dateTime
* @return
*/
public static boolean isToday(LocalDateTime dateTime) {
return getCurrentLocalDate().equals(dateTime.toLocalDate());
}
//獲取x個月前的時間點
public static String getTwoTimePoint(int x){
LocalDateTime now = LocalDateTime.now();
now = now.minus(x, ChronoUnit.MONTHS);
return now.format(DATETIME_FORMATTER);
}
/**
* 時間戳轉字符串
* 將Long類型的時間戳轉換成String 類型的時間格式,時間格式為:yyyy-MM-dd HH:mm:ss
* @param time
* @return
*/
public static String convertTimeToString(Long time){
Assert.notNull(time, "time is null");
return DATETIME_FORMATTER.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault()));
}
}
```
- JDK常用知識庫
- JDK各個版本安裝
- Java8流
- 算法
- 十大排序算法
- 冒泡排序
- 選擇排序
- 插入排序
- 歸并排序
- 快速排序
- 堆排序
- 希爾排序
- 計數排序
- 桶排序
- 基數排序
- 總結
- 常用工具類
- 浮點型計算
- 時間格式處理
- 常用功能點思路整理
- 登錄
- 高并發
- 線程安全的單例模式
- Tomcat優化
- Tomcat之APR模式
- Tomcat啟動過慢問題
- 常用的數據庫連接池
- Druid連接池
- 緩存
- Redis
- SpringBoot整合Redis
- 依賴和配置
- RedisTemplate工具類
- 工具類使用方法
- Redis知識庫
- Redis安裝
- Redis配置參數
- Redis常用Lua腳本
- MongoDB
- SpringBoot操作MongoDB
- 依賴和配置
- MongoDB工具類
- 工具類使用方法
- 消息中間件
- ActiveMq
- SpringBoot整合ActiveMq
- 框架
- SpringBoot
- 定時任務
- 啟動加載
- 事務
- JSP
- 靜態類注入
- SpringSecurity
- Shiro
- 配置及整合
- 登陸驗證
- 權限驗證
- 分布式應用
- SpringMVC
- ORM框架
- Mybatis
- 增
- 刪
- 改
- 查
- 程序員小笑話
- 我給你講一個TCP的笑話吧
- 二進制笑話
- JavaScript的那點東西
- JavaScript內置對象及常見API詳細介紹
- JavaScript實現Ajax 資源請求
- JavaScript干貨
- 架構師成長之路
- JDK源碼解析
- ArrayList源碼解讀
- 設計模式
- 微服務架構設計模式
- 逃離單體煉獄
- 服務的拆分策略
- 全面解析SpringMvc框架
- 架構設計的六大原則
- 并發集合
- JUC并發編程
- 搜索引擎
- Solr
- Solr的安裝
- 分布式服務框架
- Dubbo
- 從零開始學HTMl
- 第一章-初識HTML
- 第二章-認識HTML標簽