Java8的DateTimeFormatter與SimpleDateFormat的區(qū)別詳解
兩者最大的區(qū)別是,Java8的DateTimeFormatter是線程安全的,而SimpleDateFormat并不是線程安全。
package com.main; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; public class Main { public static void main(String args[]){ //解析日期 String dateStr= "2016年10月25日"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); LocalDate date= LocalDate.parse(dateStr, formatter); //日期轉(zhuǎn)換為字符串 LocalDateTime now = LocalDateTime.now(); DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm a"); String nowStr = now .format(format); System.out.println(nowStr); //ThreadLocal來限制SimpleDateFormat System.out.println(format(new Date())); } //要在高并發(fā)環(huán)境下能有比較好的體驗,可以使用ThreadLocal來限制SimpleDateFormat只能在線程內(nèi)共享,這樣就避免了多線程導(dǎo)致的線程安全問題。 private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() { @Override protected DateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } }; public static String format(Date date) { return threadLocal.get().format(date); } }
Java8 (LocalDateTime) 時間轉(zhuǎn)換
注意:LocalDateTime是帶時分秒的
1.將LocalDateTime轉(zhuǎn)為自定義的時間格式的字符串
public static String getDateTimeAsString(LocalDateTime localDateTime, String format) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); return localDateTime.format(formatter); }
2.將long類型的timestamp轉(zhuǎn)為LocalDateTime
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.將某時間字符串轉(zhuǎn)為自定義時間格式的LocalDateTime
public static LocalDateTime parseStringToDateTime(String time, String format) { DateTimeFormatter df = DateTimeFormatter.ofPattern(format); return LocalDateTime.parse(time, df); }
到此這篇關(guān)于Java8的DateTimeFormatter與SimpleDateFormat的區(qū)別詳解的文章就介紹到這了,更多相關(guān)Java8 DateTimeFormatter與SimpleDateFormat內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis中動態(tài)SQL,if,where,foreach的使用教程詳解
MyBatis的動態(tài)SQL是基于OGNL表達式的,它可以幫助我們方便的在SQL語句中實現(xiàn)某些邏輯。這篇文章主要介紹了Mybatis中動態(tài)SQL,if,where,foreach的使用教程,需要的朋友可以參考下2017-11-11SpringBoot里使用Servlet進行請求的實現(xiàn)示例
這篇文章主要介紹了SpringBoot里使用Servlet進行請求的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java報錯:java.util.concurrent.ExecutionException的解決辦法
在Java并發(fā)編程中,我們經(jīng)常使用java.util.concurrent包提供的工具來管理和協(xié)調(diào)多個線程的執(zhí)行,va并發(fā)編程中,然而,在使用這些工具時,可能會遇到各種各樣的異常,其中之一就是java.util.concurrent.ExecutionException,本文將詳細(xì)分析這種異常的背景、可能的原因2024-09-09