亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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)文章

  • 使用EasyExcel實(shí)現(xiàn)簡(jiǎn)單的Excel表格解析操作

    使用EasyExcel實(shí)現(xiàn)簡(jiǎn)單的Excel表格解析操作

    這篇文章主要介紹了如何使用EasyExcel完成簡(jiǎn)單的表格解析操作,同時(shí)實(shí)現(xiàn)了大量數(shù)據(jù)情況下數(shù)據(jù)的分次批量入庫,并記錄每條數(shù)據(jù)入庫的狀態(tài),感興趣的可以了解下
    2025-03-03
  • 深入理解SpringBoot中關(guān)于Mybatis使用方法

    深入理解SpringBoot中關(guān)于Mybatis使用方法

    這篇文章主要介紹了SpringBoot中關(guān)于Mybatis使用方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-03-03
  • Java數(shù)組的遍歷與求和知識(shí)點(diǎn)

    Java數(shù)組的遍歷與求和知識(shí)點(diǎn)

    本篇文章給大家總計(jì)了Java數(shù)組的遍歷與求和的知識(shí)點(diǎn)以及需要注意的地方,需要的朋友參考學(xué)習(xí)下。
    2018-02-02
  • 帶你了解Java Maven的打包操作

    帶你了解Java Maven的打包操作

    這篇文章主要介紹了Maven打包的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用案例詳解

    JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用案例詳解

    通過 JdbcTemplate 直接執(zhí)行 SQL 語句,結(jié)合源碼動(dòng)態(tài)編譯即可方便實(shí)現(xiàn)動(dòng)態(tài)修改代碼邏輯的效果,這篇文章主要介紹了JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用,需要的朋友可以參考下
    2023-09-09
  • Java TCP協(xié)議通信超詳細(xì)講解

    Java TCP協(xié)議通信超詳細(xì)講解

    TCP/IP是一種面向連接的、可靠的、基于字節(jié)流的傳輸層通信協(xié)議,它會(huì)保證數(shù)據(jù)不丟包、不亂序。TCP全名是Transmission Control Protocol,它是位于網(wǎng)絡(luò)OSI模型中的第四層
    2022-09-09
  • Java 中組合模型之對(duì)象結(jié)構(gòu)模式的詳解

    Java 中組合模型之對(duì)象結(jié)構(gòu)模式的詳解

    這篇文章主要介紹了Java 中組合模型之對(duì)象結(jié)構(gòu)模式的詳解的相關(guān)資料,希望通過本文能幫助到大家理解應(yīng)用對(duì)象結(jié)構(gòu)模型,需要的朋友可以參考下
    2017-09-09
  • 詳解Java中Collection集合的常用方法

    詳解Java中Collection集合的常用方法

    本篇文章給大家?guī)淼膬?nèi)容是關(guān)于Java中Collection集合的常用方法詳解,有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你有所幫助。下面我們就來學(xué)習(xí)一下吧
    2021-11-11
  • Java 繼承和多態(tài)的作用及好處

    Java 繼承和多態(tài)的作用及好處

    文章講解Java繼承與多態(tài)的概念、語法及應(yīng)用,繼承通過extends復(fù)用父類成員,減少冗余;多態(tài)實(shí)現(xiàn)方法重寫與向上轉(zhuǎn)型,提升靈活性與代碼復(fù)用性,動(dòng)態(tài)綁定降低圈復(fù)雜度,感興趣的朋友一起看看吧
    2025-06-06
  • 三分鐘帶你搞懂springboot原理

    三分鐘帶你搞懂springboot原理

    Spring?Boot?通過自動(dòng)配置、起步依賴和嵌入式服務(wù)器等特性,極大地簡(jiǎn)化了?Spring?應(yīng)用的開發(fā)和部署流程,其核心原理包括自動(dòng)配置機(jī)制、條件注解和配置文件加載順序等,這篇文章主要介紹了Spring?Boot?原理詳解,三分鐘帶你搞懂springboot原理,需要的朋友可以參考下
    2025-04-04

最新評(píng)論