[精讚] [會員登入]
548

java.time 時間 instant

java.time

分享此文連結 //n.sfs.tw/12356

分享連結 java.time 時間 instant@igogo
(文章歡迎轉載,務必尊重版權註明連結來源)
2022-10-12 11:34:33 最後編修
2018-04-27 09:12:43 By igogo
 

 

當使用者申請表單後, 需要為此文件產生一個申請的時間戳記

可用 java.time 中的 Instant  

Instant instant = Instant.now();

 

取得與Epoch Time 相差的秒數存到資料庫 (西元1970年1月1日凌晨0點整)

long timestamp = instant.getEpochSecond();

 

這樣會取得一個 long 值 例如: 1524790672

 

但是在使用者的表單上 列印這個只是一長串數值是沒意義的  所以要改成Asia/Taipei的時間及可悅讀的時間格式

instant = Instant.ofEpochSecond(timestamp);

ZonedDateTime present = instant.atZone(ZoneId.of("Asia/Taipei"));  //taipei時區

System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(present));

 

日後從資料庫取出顯示時間的方法亦同

 

 

抓時間四小時的記錄

  Instant fourHoursAgo = Instant.now().minus(Duration.ofHours(4));

 

END

你可能感興趣的文章

Arrays.asList 後想再add出現 UnsupportedOperationException 這個問題真是搞死我了 List<String> fruits = Arrays.asList("a

ArrayList 想移除特定值 想移出water, 使用lambda 的方式如下 List<String> fruits = new Arr

使用poi 解析 docx 原先是想解析出在docx 中的文字跟圖片, 但是, 有些我們認為是圖片, 其實是用方程式表示, 實在是太麻煩了 就記錄一

spring boot jpa 使用多個欄位排序 我有一entity 叫 team 資料欄下如下 @Id @GeneratedValue(strategy = Gener

ArrayList 與 HashMap 範例 public static void main(String[] args) { String titleIds = &

spring boot 使用jdbc連接mariadb spring boot, mariadb, jdbc, rowmapper

隨機好文

tmux 將終端機切割成多個視 窗 tmux, multiple terminal

vue.js component 在parent與child 傳值 component 在parent與child 傳值

臺中市雲端校務系統與Windows AD帳號整合(9) 3-2 Linux上隨系統開機啟動服務 以Centos 7 發行版為例 請注意, 此時提供服務的主机為Linux, 所以

spring boot jpa 使用多個欄位排序 我有一entity 叫 team 資料欄下如下 @Id @GeneratedValue(strategy = Gener

javascript 陣列 javascript 陣列可以放各种型別的元素 let data = [1,2,"john",tru