Java實現(xiàn)時間與字符串互相轉換詳解
一、日期格式化為字符串
在Java中,可以使用java.time包中的DateTimeFormatter類將日期格式化為字符串。以下是使用DateTimeFormatter的示例:
(一)使用預定義格式
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格式日期時間: " + 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("自定義格式日期時間: " + formattedDate);
}
}
二、字符串解析為日期
將字符串解析為日期時,需要確保字符串的格式與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("解析后的日期時間: " + 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("解析后的日期時間: " + date);
}
}
三、處理不同時區(qū)的日期
在處理不同時區(qū)的日期時,可以使用ZonedDateTime類。以下是將日期轉換為不同時區(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("紐約時區(qū)日期時間: " + formattedDate);
}
}
四、總結
Java的java.time包提供了強大的日期和時間處理功能,通過DateTimeFormatter可以輕松地在日期和字符串之間進行轉換。
五、方法補充
java 時間轉字符串
要將Java中的時間轉換為字符串,可以使用java.util.Date類或java.time.LocalDateTime類來表示時間,并使用java.text.SimpleDateFormat類來格式化時間。
以下是使用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轉換為字符串
String dateString = dateFormat.format(currentDate);
// 打印結果
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轉換為字符串
String dateTimeString = currentDateTime.format(formatter);
// 打印結果
System.out.println(dateTimeString);
}
}以上示例中的日期格式為"yyyy-MM-dd HH:mm:ss",你可以根據(jù)需要自定義日期格式。
java 時間與字符串之間的轉換
1. long字符串轉換成yyyy-MM-dd HH:mm:ss格式輸出
import java.text.SimpleDateFormat;
import java.util.Date;
//將long字符串轉換成格式時間輸出
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. 字符串轉換成時間
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. 取得當前系統(tǒng)時間,返回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. 取得當前系統(tǒng)時間,返回 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轉換成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);
}
} 到此這篇關于Java實現(xiàn)時間與字符串互相轉換詳解的文章就介紹到這了,更多相關Java時間與字符串互轉內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java request.getHeader("user-agent")獲取瀏覽器信息的方法
這篇文章主要介紹了java request.getHeader("user-agent")獲取瀏覽器信息的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03
java 獲取HttpRequest Header的幾種方法(必看篇)
下面小編就為大家?guī)硪黄猨ava 獲取HttpRequest Header的幾種方法(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
SpringBoot使用JWT實現(xiàn)登錄驗證的方法示例
這篇文章主要介紹了SpringBoot使用JWT實現(xiàn)登錄驗證的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-06-06
MyBatis-Plus通過version機制實現(xiàn)樂觀鎖的思路
version機制的核心思想就是,假設發(fā)生并發(fā)沖突的幾率很低,只有當更新數(shù)據(jù)的時候采取檢查是否有沖突,而判斷是否有沖突的依據(jù)就是version的值是否被改變了,這篇文章主要介紹了MyBatis-Plus通過version機制實現(xiàn)樂觀鎖的思路,需要的朋友可以參考下2021-09-09
詳解Mybatis攔截器安全加解密MySQL數(shù)據(jù)實戰(zhàn)
本文主要介紹了Mybatis攔截器安全加解密MySQL數(shù)據(jù)實戰(zhàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01

