這篇文章主要介紹“Java8中時(shí)間轉(zhuǎn)換代碼實(shí)例”,在日常操作中,相信很多人在Java8中時(shí)間轉(zhuǎn)換代碼實(shí)例問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java8中時(shí)間轉(zhuǎn)換代碼實(shí)例”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
1.將LocalDateTime轉(zhuǎn)為自定義的時(shí)間格式的字符串
public static String getDateTimeAsString(LocalDateTime localDateTime, String format) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); return localDateTime.format(formatter);}
2.將long類型的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);}
3.將LocalDateTime轉(zhuǎn)為long類型的timestamp
public static long getTimestampOfDateTime(LocalDateTime localDateTime) { ZoneId zone = ZoneId.systemDefault(); Instant instant = localDateTime.atZone(zone).toInstant(); return instant.toEpochMilli();}
4.將某時(shí)間字符串轉(zhuǎn)為自定義時(shí)間格式的LocalDateTime
public static LocalDateTime parseStringToDateTime(String time, String format) { DateTimeFormatter df = DateTimeFormatter.ofPattern(format); return LocalDateTime.parse(time, df);}
到此,關(guān)于“Java8中時(shí)間轉(zhuǎn)換代碼實(shí)例”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
本文名稱:Java8中時(shí)間轉(zhuǎn)換代碼實(shí)例-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://www.rwnh.cn/article28/ijgjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、用戶體驗(yàn)、網(wǎng)站改版、微信小程序、網(wǎng)站設(shè)計(jì)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容