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

Java中字符串轉(zhuǎn)時(shí)間與時(shí)間轉(zhuǎn)字符串的操作詳解

 更新時(shí)間:2025年04月25日 10:22:43   作者:Java皇帝  
Java 的 java.time 包提供了強(qiáng)大的日期和時(shí)間處理功能,通過 DateTimeFormatter 可以輕松地在日期時(shí)間對(duì)象和字符串之間進(jìn)行轉(zhuǎn)換,下面小編給大家詳細(xì)介紹一下Java中字符串轉(zhuǎn)時(shí)間與時(shí)間轉(zhuǎn)字符串的操作,需要的朋友可以參考下

一、字符串轉(zhuǎn)時(shí)間

在 Java 中,可以使用 java.time 包中的 DateTimeFormatter 類將字符串格式的日期時(shí)間轉(zhuǎn)換為 LocalDateTime 或 ZonedDateTime 對(duì)象。

(一)使用預(yù)定義格式

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class StringToDateExample {
    public static void main(String[] args) {
        String isoDateTime = "2023-10-11T12:34:56";
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        LocalDateTime dateTime = LocalDateTime.parse(isoDateTime, formatter);
        System.out.println("解析后的日期時(shí)間: " + dateTime);
    }
}

(二)自定義格式

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class CustomStringToDateExample {
    public static void main(String[] args) {
        String customDateTime = "2023-10-11 12:34:56";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(customDateTime, formatter);
        System.out.println("解析后的日期時(shí)間: " + dateTime);
    }
}

二、時(shí)間轉(zhuǎn)字符串

將日期時(shí)間對(duì)象格式化為字符串,可以使用 DateTimeFormatter 類。

(一)使用預(yù)定義格式

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateToStringExample {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        String formattedDate = now.format(formatter);
        System.out.println("ISO格式日期時(shí)間: " + formattedDate);
    }
}

(二)自定義格式

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class CustomDateToStringExample {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDate = now.format(formatter);
        System.out.println("自定義格式日期時(shí)間: " + formattedDate);
    }
}

三、處理不同時(shí)區(qū)的日期

在處理不同時(shí)區(qū)的日期時(shí),可以使用 ZonedDateTime 類。

(一)字符串轉(zhuǎn)帶時(shí)區(qū)的日期時(shí)間

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class StringToZonedDateTimeExample {
    public static void main(String[] args) {
        String zonedDateTimeString = "2023-10-11T12:34:56-04:00";
        DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(zonedDateTimeString, formatter);
        System.out.println("解析后的帶時(shí)區(qū)日期時(shí)間: " + zonedDateTime);
    }
}

(二)帶時(shí)區(qū)的日期時(shí)間轉(zhuǎn)字符串

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ZonedDateTimeToStringExample {
    public static void main(String[] args) {
        ZonedDateTime now = ZonedDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
        String formattedDate = now.format(formatter);
        System.out.println("帶時(shí)區(qū)的日期時(shí)間字符串: " + formattedDate);
    }
}

四、總結(jié)

Java 的 java.time 包提供了強(qiáng)大的日期和時(shí)間處理功能,通過 DateTimeFormatter 可以輕松地在日期時(shí)間對(duì)象和字符串之間進(jìn)行轉(zhuǎn)換。

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

相關(guān)文章

  • SpringBoot集成Kafka并使用多個(gè)死信隊(duì)列詳解

    SpringBoot集成Kafka并使用多個(gè)死信隊(duì)列詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot集成Kafka并使用多個(gè)死信隊(duì)列的示例代碼,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-05-05
  • springboot過濾器執(zhí)行兩次的解決及跨域過濾器問題

    springboot過濾器執(zhí)行兩次的解決及跨域過濾器問題

    這篇文章主要介紹了springboot過濾器執(zhí)行兩次的解決及跨域過濾器問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java多線程編程之CountDownLatch同步工具使用實(shí)例

    Java多線程編程之CountDownLatch同步工具使用實(shí)例

    這篇文章主要介紹了Java多線程編程之CountDownLatch同步工具使用實(shí)例,需要的朋友可以參考下
    2015-05-05
  • Java驗(yàn)證日期時(shí)間字符串是否合法的三種方式

    Java驗(yàn)證日期時(shí)間字符串是否合法的三種方式

    判斷日期經(jīng)常合法出現(xiàn)在IO場(chǎng)景下,下面將盡量使用簡(jiǎn)練的思路和代碼呈現(xiàn)給大伙,這篇文章主要給大家介紹了關(guān)于Java驗(yàn)證日期時(shí)間字符串是否合法的三種方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • Java多態(tài)向上轉(zhuǎn)型和向下轉(zhuǎn)型及優(yōu)點(diǎn)總結(jié)

    Java多態(tài)向上轉(zhuǎn)型和向下轉(zhuǎn)型及優(yōu)點(diǎn)總結(jié)

    多態(tài)指不同對(duì)象對(duì)同一方法呈現(xiàn)不同行為,通過向上轉(zhuǎn)型和重寫實(shí)現(xiàn),提升代碼靈活性與可擴(kuò)展性,降低復(fù)雜度,這篇文章主要介紹了Java多態(tài)向上轉(zhuǎn)型和向下轉(zhuǎn)型及優(yōu)點(diǎn)的相關(guān)資料,需要的朋友可以參考下
    2025-06-06
  • Java8新特性:函數(shù)式編程

    Java8新特性:函數(shù)式編程

    Java8最新引入函數(shù)式編程概念,該項(xiàng)技術(shù)可以大大提升編碼效率,本文會(huì)對(duì)涉及的對(duì)象等進(jìn)行兩種方法的對(duì)比,對(duì)新技術(shù)更直白的看到變化,更方便學(xué)習(xí)
    2021-06-06
  • Java字符串中有多個(gè)分隔符的處理方式

    Java字符串中有多個(gè)分隔符的處理方式

    這篇文章主要介紹了Java字符串中有多個(gè)分隔符的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-05-05
  • EditPlus運(yùn)行java時(shí)從鍵盤輸入數(shù)據(jù)的操作方法

    EditPlus運(yùn)行java時(shí)從鍵盤輸入數(shù)據(jù)的操作方法

    這篇文章主要介紹了EditPlus運(yùn)行java時(shí)從鍵盤輸入數(shù)據(jù)的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • 使用MyBatis 動(dòng)態(tài)update數(shù)據(jù)

    使用MyBatis 動(dòng)態(tài)update數(shù)據(jù)

    使用mybatis寫sql,需要?jiǎng)討B(tài)更新對(duì)象數(shù)據(jù),每次需要更新的字段不同,為了防止null空異常,就需要用動(dòng)態(tài)sql了,下面給大家分享一段代碼關(guān)于mybatis動(dòng)態(tài)update,需要的朋友參考下
    2016-11-11
  • Java中的自動(dòng)裝箱與自動(dòng)拆箱的實(shí)現(xiàn)

    Java中的自動(dòng)裝箱與自動(dòng)拆箱的實(shí)現(xiàn)

    自動(dòng)裝箱和自動(dòng)拆箱使得我們?cè)谑褂没緮?shù)據(jù)類型時(shí)更加方便,同時(shí)也提高了代碼的可讀性和健壯性,本文將詳細(xì)介紹Java中的自動(dòng)裝箱和自動(dòng)拆箱機(jī)制,感興趣的可以了解一下
    2023-08-08

最新評(píng)論

崇义县| 孟州市| 阳山县| 和龙市| 兴安盟| 上高县| 诸暨市| 两当县| 扶风县| 商丘市| 大悟县| 肥乡县| 东乌珠穆沁旗| 扎囊县| 江津市| 观塘区| SHOW| 三门峡市| 绍兴县| 龙泉市| 望都县| 镇坪县| 竹溪县| 静宁县| 德庆县| 桦南县| 安龙县| 牟定县| 邻水| 泰顺县| 武定县| 铅山县| 即墨市| 藁城市| 石屏县| 莱州市| 哈尔滨市| 南郑县| 阜阳市| 额济纳旗| 沙洋县|