欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java8的DateTimeFormatter與SimpleDateFormat的區(qū)別詳解

 更新時間:2021年03月17日 10:34:24   作者:二十六畫生的博客  
這篇文章主要介紹了Java8的DateTimeFormatter與SimpleDateFormat的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

兩者最大的區(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)文章

  • eclipse 安裝lombok插件

    eclipse 安裝lombok插件

    這篇文章主要介紹了eclipse 安裝lombok插件的詳細(xì)步驟,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • MyBatis別名和settings設(shè)置方式

    MyBatis別名和settings設(shè)置方式

    這篇文章主要介紹了MyBatis別名和settings設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Maven引入本地Jar包并打包進War包中的方法

    Maven引入本地Jar包并打包進War包中的方法

    本篇文章主要介紹了Maven引入本地Jar包并打包進War包中的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Mybatis中動態(tài)SQL,if,where,foreach的使用教程詳解

    Mybatis中動態(tài)SQL,if,where,foreach的使用教程詳解

    MyBatis的動態(tài)SQL是基于OGNL表達式的,它可以幫助我們方便的在SQL語句中實現(xiàn)某些邏輯。這篇文章主要介紹了Mybatis中動態(tài)SQL,if,where,foreach的使用教程,需要的朋友可以參考下
    2017-11-11
  • SpringBoot里使用Servlet進行請求的實現(xiàn)示例

    SpringBoot里使用Servlet進行請求的實現(xiàn)示例

    這篇文章主要介紹了SpringBoot里使用Servlet進行請求的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java代碼中4種字符串拼接方式分析

    Java代碼中4種字符串拼接方式分析

    本文主要介紹了Java代碼中4種字符串拼接方式分析,主要介紹了“+”號、StringBuilder、StringJoiner、String#join,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 一文詳解java閉包的用途是什么

    一文詳解java閉包的用途是什么

    閉包的價值在于可以作為函數(shù)對象或者匿名函數(shù),持有上下文數(shù)據(jù),作為第一級對象進行傳遞和保存,下面這篇文章主要給大家介紹了關(guān)于java閉包的用途是什么,需要的朋友可以參考下
    2024-03-03
  • Java 封裝的使用詳解

    Java 封裝的使用詳解

    在面向?qū)ο蟪淌皆O(shè)計方法中,封裝(英語:Encapsulation)是指一種將抽象性函式接口的實現(xiàn)細(xì)節(jié)部分包裝、隱藏起來的方法。封裝可以被認(rèn)為是一個保護屏障,防止該類的代碼和數(shù)據(jù)被外部類定義的代碼隨機訪問。要訪問該類的代碼和數(shù)據(jù),必須通過嚴(yán)格的接口控制
    2021-11-11
  • Java報錯:java.util.concurrent.ExecutionException的解決辦法

    Java報錯: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
  • hibernate多表操作實例代碼

    hibernate多表操作實例代碼

    這篇文章主要介紹了hibernate多表操作實例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02

最新評論