Java實(shí)現(xiàn)時(shí)間與字符串互相轉(zhuǎn)換詳解
一、日期格式化為字符串
在Java中,可以使用java.time包中的DateTimeFormatter類將日期格式化為字符串。以下是使用DateTimeFormatter的示例:
(一)使用預(yù)定義格式
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateFormatExample {
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 CustomFormatExample {
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í),需要確保字符串的格式與DateTimeFormatter指定的格式一致。以下是解析字符串為日期的示例:
(一)解析ISO格式字符串
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ParseISODateExample {
public static void main(String[] args) {
String isoDate = "2023-10-11T12:34:56";
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
LocalDateTime date = LocalDateTime.parse(isoDate, formatter);
System.out.println("解析后的日期時(shí)間: " + date);
}
}
(二)解析自定義格式字符串
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ParseCustomDateExample {
public static void main(String[] args) {
String customDate = "2023-10-11 12:34:56";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date = LocalDateTime.parse(customDate, formatter);
System.out.println("解析后的日期時(shí)間: " + date);
}
}
三、處理不同時(shí)區(qū)的日期
在處理不同時(shí)區(qū)的日期時(shí),可以使用ZonedDateTime類。以下是將日期轉(zhuǎn)換為不同時(shí)區(qū)的示例:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
???????public class TimeZoneExample {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
ZoneId zoneId = ZoneId.of("America/New_York");
ZonedDateTime zonedDateTime = ZonedDateTime.of(now, zoneId);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
String formattedDate = zonedDateTime.format(formatter);
System.out.println("紐約時(shí)區(qū)日期時(shí)間: " + formattedDate);
}
}
四、總結(jié)
Java的java.time包提供了強(qiáng)大的日期和時(shí)間處理功能,通過(guò)DateTimeFormatter可以輕松地在日期和字符串之間進(jìn)行轉(zhuǎn)換。
五、方法補(bǔ)充
java 時(shí)間轉(zhuǎn)字符串
要將Java中的時(shí)間轉(zhuǎn)換為字符串,可以使用java.util.Date類或java.time.LocalDateTime類來(lái)表示時(shí)間,并使用java.text.SimpleDateFormat類來(lái)格式化時(shí)間。
以下是使用java.util.Date和java.text.SimpleDateFormat的示例代碼:
import java.util.Date;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
Date currentDate = new Date();
// 指定日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 將Date轉(zhuǎn)換為字符串
String dateString = dateFormat.format(currentDate);
// 打印結(jié)果
System.out.println(dateString);
}
}以下是使用java.time.LocalDateTime和java.time.format.DateTimeFormatter的示例代碼:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
// 指定日期格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 將LocalDateTime轉(zhuǎn)換為字符串
String dateTimeString = currentDateTime.format(formatter);
// 打印結(jié)果
System.out.println(dateTimeString);
}
}以上示例中的日期格式為"yyyy-MM-dd HH:mm:ss",你可以根據(jù)需要自定義日期格式。
java 時(shí)間與字符串之間的轉(zhuǎn)換
1. long字符串轉(zhuǎn)換成yyyy-MM-dd HH:mm:ss格式輸出
import java.text.SimpleDateFormat;
import java.util.Date;
//將long字符串轉(zhuǎn)換成格式時(shí)間輸出
public class LongToString {
public static void main(String argsp[]){
String time="1256006105375";
Date date=new Date(Long.parseLong(time));
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
time=formatter.format(date);
System.out.println(time);
}
} 2. 字符串轉(zhuǎn)換成時(shí)間
import java.text.SimpleDateFormat;
import java.util.Date;
import ognl.ParseException;
public class StringToDate {
public static void main(String argsp[]) throws Exception{
String time="2010-11-20 11:10:10";
Date date=null;
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date=formatter.parse(time);
System.out.println(date);
}
} 3. 取得當(dāng)前系統(tǒng)時(shí)間,返回yyyy-MM-dd HH:mm:ss字符串
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate {
public static void main(String argsp[]) throws Exception{
Date date=new Date();
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=formatter.format(date);
System.out.println(time);
}
} 4. 取得當(dāng)前系統(tǒng)時(shí)間,返回 HH:mm:ss字符串
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate {
public static void main(String argsp[]) throws Exception{
Date date=new Date();
SimpleDateFormat formatter=new SimpleDateFormat("HH:mm:ss");
String time=formatter.format(date);
System.out.println(time);
}
} 5.將20101125102503轉(zhuǎn)換成2010-11-25 10:25:03輸出
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate {
public static void main(String argsp[]) throws Exception{
String time="20101125102503";
SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-HH-dd HH:mm:ss");
SimpleDateFormat formatter2=new SimpleDateFormat("yyyyHHddHHmmss");
time=formatter1.format(formatter2.parse(time));
System.out.println(time);
}
} 到此這篇關(guān)于Java實(shí)現(xiàn)時(shí)間與字符串互相轉(zhuǎn)換詳解的文章就介紹到這了,更多相關(guān)Java時(shí)間與字符串互轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java8中forEach語(yǔ)句循環(huán)一個(gè)List和Map
這篇文章主要給大家介紹了關(guān)于Java8中forEach語(yǔ)句循環(huán)一個(gè)List和Map的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Spring啟動(dòng)指定時(shí)區(qū)的兩種方法
最近項(xiàng)目啟動(dòng),時(shí)間要修改成東七區(qū)時(shí)間,本文主要介紹了Spring啟動(dòng)指定時(shí)區(qū)的兩種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
Java上傳文件到服務(wù)器指定文件夾實(shí)現(xiàn)過(guò)程圖解
這篇文章主要介紹了Java上傳文件到服務(wù)器指定文件夾實(shí)現(xiàn)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Java實(shí)現(xiàn)上傳文件圖片到指定服務(wù)器目錄
本文通過(guò)實(shí)例代碼給大家介紹了java上傳文件圖片到指定服務(wù)器目錄的相關(guān)知識(shí),代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
Java中s.charAt(index)用于提取字符串s中的特定字符操作
這篇文章主要介紹了Java中s.charAt(index)用于提取字符串s中的特定字符操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
JAVA設(shè)計(jì)模式零基礎(chǔ)解析之單例模式的八種方式
設(shè)計(jì)模式(Design pattern)是一套被反復(fù)使用、多數(shù)人知曉的、經(jīng)過(guò)分類編目的、代碼設(shè)計(jì)經(jīng)驗(yàn)的總結(jié)。使用設(shè)計(jì)模式是為了可重用代碼、讓代碼更容易被他人理解、保證代碼可靠性2021-10-10
Springboot使用@WebListener?作為web監(jiān)聽器的過(guò)程解析
Mybatis的動(dòng)態(tài)Sql組合模式詳情

