最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)時(shí)間和字符串互轉(zhuǎn)

 更新時(shí)間:2024年10月30日 11:17:35   作者:tomorrow.hello  
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Java實(shí)現(xiàn)時(shí)間對(duì)象和字符串互相轉(zhuǎn)換,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1.當(dāng)前時(shí)間對(duì)象轉(zhuǎn)字符串

方法一:Date和SimpleDateFormat實(shí)現(xiàn)

使用java.util.Date類(lèi)和java.text.SimpleDateFormat類(lèi)

public static void main(String[] args) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(now);
        System.out.println(currentTime);
    }

方法二:LocalDateTime和DateTimeFormatter

使用java.time.LocalDateTime類(lèi)和java.time.format.DateTimeFormatter類(lèi)(Java 8及以上版本)

    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String currentTime = now.format(dtf);
        System.out.println(currentTime);
    }

方式三:Calendar

public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        String currentTime = String.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
        System.out.println(currentTime);
    }

方式四:LocalDate和LocalTime

使用java.time.LocalDate類(lèi)和java.time.LocalTime類(lèi)(Java 8及以上版本)

 public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        LocalTime currentTime = LocalTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDate = currentDate.format(dtf);
        String formattedTime = currentTime.format(dtf);
        System.out.println(formattedDate + " " + formattedTime);
    }

2.時(shí)間字符串轉(zhuǎn)時(shí)間對(duì)象

方式一: SimpleDateFormat

        String dateString = "2024-10-28 22:56:11";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.parse(dateString);

方式二:DateTimeFormatter

DateTimeFormatter有三種解析模式:

STRICT:嚴(yán)格模式,日期、時(shí)間必須完全正確。

SMART:智能模式,針對(duì)日可以自動(dòng)調(diào)整。月的范圍在 1 到 12,日的范圍在 1 到 31。比如輸入是 2 月 30 號(hào),當(dāng)年 2 月只有 28 天,返回的日期就是 2 月 28 日。默認(rèn)模式

LENIENT:寬松模式,主要針對(duì)月和日,會(huì)自動(dòng)后延。結(jié)果類(lèi)似于LocalData#plusDays或者LocalDate#plusMonths。

SMART智能模式:

        testTime("2023-02-33 22:56:11");
        testTime("2023-02-30 22:56:11");    
 
 
private static void testTime(String dateString){
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        try {
            LocalDateTime localDateTime = LocalDateTime.parse(dateString, sdf);
            System.out.println("轉(zhuǎn)換后時(shí)間:" + localDateTime);
        } catch (Exception ex) {
            System.err.println("轉(zhuǎn)換失敗:" + ex.getMessage());
        }
    }

LENIENT寬松模式:

        testTime("2023-02-33 22:56:11");
        testTime("2023-02-30 22:56:11");
 
   
 private static void testTime(String dateString){
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                .withResolverStyle(ResolverStyle.LENIENT);
        try {
            LocalDateTime localDateTime = LocalDateTime.parse(dateString, sdf);
            System.out.println("轉(zhuǎn)換后時(shí)間:" + localDateTime);
        } catch (Exception ex) {
            System.err.println("轉(zhuǎn)換失敗:" + ex.getMessage());
        }
    }

STRICT嚴(yán)格模式:

嚴(yán)格模式下時(shí)間字符串的年份格式化分為yyyy和uuuu兩種。當(dāng)DateTimeFormatter.ofPattern()進(jìn)行非嚴(yán)格模式下的格式化的時(shí)候,yyyy/MM/dd和uuuu/MM/dd表現(xiàn)相同,都是轉(zhuǎn)換為合法的日期。

yyyy:

yyyy代表公元紀(jì)年,在嚴(yán)格模式下,如果時(shí)間字符串不包含公元,因此程序報(bào)錯(cuò)。

        testTime("2023-02-20 22:56:11");
        testTime("2023-02-30 22:56:11");
   
 private static void testTime(String dateString){
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                .withResolverStyle(ResolverStyle.STRICT);
        try {
            LocalDateTime localDateTime = LocalDateTime.parse(dateString, sdf);
            System.out.println("轉(zhuǎn)換后時(shí)間:" + localDateTime);
        } catch (Exception ex) {
            System.err.println("轉(zhuǎn)換失敗:" + ex.getMessage());
        }
    }

使用G yyyy/MM/dd來(lái)進(jìn)行格式化 ,并且在時(shí)間字符串中也增加了公元單位。

        testTime("公元 2023-02-20 22:56:11");
        testTime("公元 2023-02-30 22:56:11");
    
 
private static void testTime(String dateString){
        /**
         *  Locale.JAPAN  --> "西暦 yyyy-MM-dd"
         *  Locale.CHINA  --> "公元 yyyy-MM-dd"
         *  Locale.US     --> "AD yyyy-MM-dd"
         */
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("G yyyy-MM-dd HH:mm:ss",Locale.CHINESE)
                .withResolverStyle(ResolverStyle.STRICT);
        try {
            LocalDateTime localDateTime = LocalDateTime.parse(dateString, sdf);
            System.out.println("轉(zhuǎn)換后時(shí)間:" + localDateTime);
        } catch (Exception ex) {
            System.err.println("轉(zhuǎn)換失敗:" + ex.getMessage());
        }
    }

uuuu: 

uuuu不代表公元格式化,所以不會(huì)存在異常。

 
        testTime("2023-02-20 22:56:11");
        testTime("2023-02-29 22:56:11");
 
  
private static void testTime(String dateString){
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss")
                .withResolverStyle(ResolverStyle.STRICT);
        try {
            LocalDateTime localDateTime = LocalDateTime.parse(dateString, sdf);
            System.out.println("轉(zhuǎn)換后時(shí)間:" + localDateTime);
        } catch (Exception ex) {
            System.err.println("轉(zhuǎn)換失敗:" + ex);
        }
    }

到此這篇關(guān)于Java實(shí)現(xiàn)時(shí)間和字符串互轉(zhuǎn)的文章就介紹到這了,更多相關(guān)Java時(shí)間和字符串互轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot啟動(dòng)security后如何關(guān)閉彈出的/login頁(yè)面

    SpringBoot啟動(dòng)security后如何關(guān)閉彈出的/login頁(yè)面

    這篇文章主要介紹了SpringBoot啟動(dòng)security后如何關(guān)閉彈出的login頁(yè)面問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java中this關(guān)鍵字的用法詳解

    Java中this關(guān)鍵字的用法詳解

    我知道很多朋友都和我一樣,在JAVA程序中似乎經(jīng)常見(jiàn)到this,自己也偶爾用到它,但是到底this該怎么用,卻心中無(wú)數(shù),下面這篇文章主要給大家介紹了關(guān)于Java中this關(guān)鍵字用法的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • Java實(shí)現(xiàn)單線程聊天室

    Java實(shí)現(xiàn)單線程聊天室

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)單線程聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Java二維數(shù)組計(jì)算集合總結(jié)

    Java二維數(shù)組計(jì)算集合總結(jié)

    本篇文章給大家整理了關(guān)于Java二維數(shù)組計(jì)算集合的內(nèi)容總結(jié),有需要的讀者們可以參考下。
    2018-02-02
  • java.lang.OutOfMemoryError: Metaspace異常解決的方法

    java.lang.OutOfMemoryError: Metaspace異常解決的方法

    這篇文章主要介紹了java.lang.OutOfMemoryError: Metaspace異常解決的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 淺談什么是SpringBoot異常處理自動(dòng)配置的原理

    淺談什么是SpringBoot異常處理自動(dòng)配置的原理

    今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著SpringBoot異常處理自動(dòng)配置展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 如何解決IDEA.properties文件中文亂碼問(wèn)題

    如何解決IDEA.properties文件中文亂碼問(wèn)題

    這篇文章主要介紹了如何解決IDEA.properties文件中文亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • SpringSecurity的防Csrf攻擊實(shí)現(xiàn)代碼解析

    SpringSecurity的防Csrf攻擊實(shí)現(xiàn)代碼解析

    這篇文章主要介紹了SpringSecurity的防Csrf攻擊實(shí)現(xiàn)代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Java文件下載ZIP報(bào)錯(cuò):Out of Memory的問(wèn)題排查

    Java文件下載ZIP報(bào)錯(cuò):Out of Memory的問(wèn)題排查

    本文主要介紹了Java項(xiàng)目中下載大文件(超過(guò)2G的ZIP文件)時(shí)出現(xiàn)內(nèi)存溢出(OutOfMemory:JavaHeapSpace)的問(wèn)題,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-01-01
  • 基于Java制作一個(gè)簡(jiǎn)易的遠(yuǎn)控終端

    基于Java制作一個(gè)簡(jiǎn)易的遠(yuǎn)控終端

    這篇文章主要為大家詳細(xì)介紹了如何基于Java制作一個(gè)簡(jiǎn)易的遠(yuǎn)控終端,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的可以了解一下
    2023-04-04

最新評(píng)論

博爱县| 东莞市| 荆州市| 高阳县| 淮南市| 玉龙| 汕尾市| 石景山区| 修武县| 上栗县| 公主岭市| 陵水| 广东省| 绥中县| 阳泉市| 长沙县| 珲春市| 门源| 兰溪市| 云阳县| 兴城市| 浮梁县| 南丹县| 怀远县| 儋州市| 师宗县| 昆明市| 浪卡子县| 玉山县| 宜黄县| 武义县| 忻州市| 包头市| 阿瓦提县| 玉环县| 资源县| 赫章县| 浦县| 徐闻县| 清远市| 克什克腾旗|