JAVA時(shí)間類型轉(zhuǎn)換處理方式
LocalDateDate
Java時(shí)間類型詳解:LocalDate與Date互轉(zhuǎn)及字符串格式化
在Java中,時(shí)間處理是一個(gè)常見的需求。隨著Java 8引入了新的日期時(shí)間API(java.time包),開發(fā)者可以更方便地處理日期和時(shí)間。本文將詳細(xì)介紹 LocalDate 與 Date 類型的互轉(zhuǎn)、字符串格式化以及其他常見時(shí)間類型的轉(zhuǎn)換。
一、LocalDate與Date類型互轉(zhuǎn)
1.Date轉(zhuǎn)LocalDate
Date 是Java早期的日期時(shí)間類,而 LocalDate 是Java 8引入的日期類。我們可以通過以下方式將 Date 轉(zhuǎn)換為 LocalDate:
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class DateToLocalDate {
public static void main(String[] args) {
// 獲取當(dāng)前Date對(duì)象
Date nowDate = new Date();
// 將Date轉(zhuǎn)換為L(zhǎng)ocalDate
LocalDate localDate = nowDate.toInstant() // 將Date轉(zhuǎn)換為Instant
.atZone(ZoneId.systemDefault()) // 指定時(shí)區(qū)
.toLocalDate(); // 轉(zhuǎn)換為L(zhǎng)ocalDate
System.out.println("Date: " + nowDate);
System.out.println("LocalDate: " + localDate);
}
}輸出示例:
Date: Mon Oct 09 12:34:56 CST 2023
LocalDate: 2023-10-09
2.LocalDate轉(zhuǎn)Date
將 LocalDate 轉(zhuǎn)換為 Date 需要借助 ZonedDateTime 或 Instant:
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class LocalDateToDate {
public static void main(String[] args) {
// 獲取當(dāng)前LocalDate對(duì)象
LocalDate localDate = LocalDate.now();
// 將LocalDate轉(zhuǎn)換為Date
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
System.out.println("LocalDate: " + localDate);
System.out.println("Date: " + date);
}
}輸出示例:
LocalDate: 2023-10-09
Date: Mon Oct 09 00:00:00 CST 2023
二、字符串格式化與解析
1.LocalDate與字符串互轉(zhuǎn)
(1)LocalDate轉(zhuǎn)字符串
使用 DateTimeFormatter 可以將 LocalDate 格式化為字符串:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateToString {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
// 定義格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 格式化LocalDate為字符串
String dateString = localDate.format(formatter);
System.out.println("LocalDate: " + localDate);
System.out.println("Formatted String: " + dateString);
}
}輸出示例:
LocalDate: 2023-10-09
Formatted String: 2023-10-09
(2)字符串轉(zhuǎn)LocalDate
將字符串解析為 LocalDate:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class StringToLocalDate {
public static void main(String[] args) {
String dateString = "2023-10-09";
// 定義格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 解析字符串為L(zhǎng)ocalDate
LocalDate localDate = LocalDate.parse(dateString, formatter);
System.out.println("String: " + dateString);
System.out.println("LocalDate: " + localDate);
}
}輸出示例:
String: 2023-10-09
LocalDate: 2023-10-09
2.Date與字符串互轉(zhuǎn)
(1)Date轉(zhuǎn)字符串
使用 SimpleDateFormat 可以將 Date 格式化為字符串:
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToString {
public static void main(String[] args) {
Date nowDate = new Date();
// 定義格式化器
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 格式化Date為字符串
String dateString = formatter.format(nowDate);
System.out.println("Date: " + nowDate);
System.out.println("Formatted String: " + dateString);
}
}輸出示例:
Date: Mon Oct 09 12:34:56 CST 2023
Formatted String: 2023-10-09 12:34:56
(2)字符串轉(zhuǎn)Date
將字符串解析為 Date:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate {
public static void main(String[] args) throws ParseException {
String dateString = "2023-10-09 12:34:56";
// 定義格式化器
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 解析字符串為Date
Date date = formatter.parse(dateString);
System.out.println("String: " + dateString);
System.out.println("Date: " + date);
}
}輸出示例:
String: 2023-10-09 12:34:56
Date: Mon Oct 09 12:34:56 CST 2023
三、其他常見時(shí)間類型轉(zhuǎn)換
1.LocalDateTime與Date互轉(zhuǎn)
(1)Date轉(zhuǎn)LocalDateTime
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class DateToLocalDateTime {
public static void main(String[] args) {
Date nowDate = new Date();
// 將Date轉(zhuǎn)換為L(zhǎng)ocalDateTime
LocalDateTime localDateTime = nowDate.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
System.out.println("Date: " + nowDate);
System.out.println("LocalDateTime: " + localDateTime);
}
}(2)LocalDateTime轉(zhuǎn)Date
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class LocalDateTimeToDate {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
// 將LocalDateTime轉(zhuǎn)換為Date
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("LocalDateTime: " + localDateTime);
System.out.println("Date: " + date);
}
}2.LocalTime與Date互轉(zhuǎn)
(1)Date轉(zhuǎn)LocalTime
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
public class DateToLocalTime {
public static void main(String[] args) {
Date nowDate = new Date();
// 將Date轉(zhuǎn)換為L(zhǎng)ocalTime
LocalTime localTime = nowDate.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalTime();
System.out.println("Date: " + nowDate);
System.out.println("LocalTime: " + localTime);
}
}(2)LocalTime轉(zhuǎn)Date
由于 LocalTime 只包含時(shí)間信息,轉(zhuǎn)換為 Date 時(shí)需要結(jié)合日期:
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
public class LocalTimeToDate {
public static void main(String[] args) {
LocalTime localTime = LocalTime.now();
// 將LocalTime轉(zhuǎn)換為Date(需要結(jié)合日期)
Date date = Date.from(LocalDate.now()
.atTime(localTime)
.atZone(ZoneId.systemDefault())
.toInstant());
System.out.println("LocalTime: " + localTime);
System.out.println("Date: " + date);
}
}到此這篇關(guān)于JAVA時(shí)間類型轉(zhuǎn)換處理方式的文章就介紹到這了,更多相關(guān)java時(shí)間類型轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中==符號(hào)與equals()的使用詳解(測(cè)試兩個(gè)變量是否相等)
下面小編就為大家?guī)硪黄狫ava中==符號(hào)與equals()的使用詳解(測(cè)試兩個(gè)變量是否相等)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
SpringCloud Gateway自動(dòng)裝配實(shí)現(xiàn)流程詳解
Spring Cloud Gateway旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單有效的、統(tǒng)一的 API 路由管理方式。Spring Cloud Gateway 作為 Spring Cloud 生態(tài)系中的網(wǎng)關(guān),它不僅提供統(tǒng)一的路由方式,并且基于 Filter 鏈的方式提供了網(wǎng)關(guān)基本的功能,例如:安全、監(jiān)控/埋點(diǎn)和限流等2022-10-10
詳解Mybatis內(nèi)的mapper方法為何不能重載
這篇文章主要介紹了詳解Mybatis內(nèi)的mapper方法為何不能重載,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):循環(huán)鏈表和棧
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表、棧的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Java數(shù)據(jù)結(jié)構(gòu)中循環(huán)鏈表、棧、的功能、定義及使用方法,需要的朋友可以參考下2021-08-08
Spring Data Jpa+SpringMVC+Jquery.pagination.js實(shí)現(xiàn)分頁示例
本文介紹了Spring Data Jpa+SpringMVC+Jquery.pagination.js實(shí)現(xiàn)分頁示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Java Stream map, Collectors(toMap, toLis
這篇文章主要介紹了Java Stream map, Collectors(toMap, toList, toSet, groupingBy, collectingAndThen)使用案例,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09

