一、獲取當(dāng)前時(shí)區(qū)時(shí)間:
Instant.now().atZone(ZoneId.systemDefault())
二、創(chuàng)建Instant實(shí)例
Instant now = Instant.now();
System.out.println(“now:”+now);
控制臺(tái)輸出:
now:2018-07-09T08:59:08.853Z
注意:通過(guò)這種方式獲取的時(shí)間戳與北京時(shí)間相差8個(gè)時(shí)區(qū),需要修正為北京時(shí)間,通過(guò)查看源代碼發(fā)現(xiàn)Instant.now()使用等是UTC時(shí)間Clock.systemUTC().instant()。LocalDate、LocalDateTime 的now()方法使用的是系統(tǒng)默認(rèn)時(shí)區(qū) 不存在Instant.now()的時(shí)間問(wèn)題。
###解決方法
增加8個(gè)小時(shí)
Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println(“now:”+now);
控制臺(tái)輸出:
now:2018-07-09T16:58:48.188Z
三、Instant獲取long類(lèi)型的10位秒數(shù)、13位毫秒數(shù)
Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)); System.out.println(“秒數(shù):”+now.getEpochSecond()); System.out.println(“毫秒數(shù):”+now.toEpochMilli());
分享文章:java8Instant時(shí)間及轉(zhuǎn)換操作-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://m.newbst.com/article28/dcspjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)頁(yè)設(shè)計(jì)公司、電子商務(wù)、移動(dòng)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容