Java中SimpleDateFormat用法詳解
public class SimpleDateFormat extends DateFormat
SimpleDateFormat 是一個(gè)以國(guó)別敏感的方式格式化和分析數(shù)據(jù)的具體類(lèi)。 它允許格式化 (date -> text)、語(yǔ)法分析 (text -> date)和標(biāo)準(zhǔn)化。
SimpleDateFormat 允許以為日期-時(shí)間格式化選擇任何用戶(hù)指定的方式啟動(dòng)。 但是,希望用 DateFormat 中的 getTimeInstance、 getDateInstance 或 getDateTimeInstance 創(chuàng)建一個(gè)日期-時(shí)間格式化程序。 每個(gè)類(lèi)方法返回一個(gè)以缺省格式化方式初始化的日期/時(shí)間格式化程序。 可以根據(jù)需要用 applyPattern 方法修改格式化方式。
SimpleDateFormat函數(shù)的繼承關(guān)系:
Java.lang.Object
|
+----java.text.Format
|
+----java.text.DateFormat
|
+----java.text.SimpleDateFormat
下面是個(gè)小例子:
import java.text.*;
import java.util.Date;
/**
SimpleDateFormat函數(shù)語(yǔ)法:
G 年代標(biāo)志符
y 年
M 月
d 日
h 時(shí) 在上午或下午 (1~12)
H 時(shí) 在一天中 (0~23)
m 分
s 秒
S 毫秒
E 星期
D 一年中的第幾天
F 一月中第幾個(gè)星期幾
w 一年中第幾個(gè)星期
W 一月中第幾個(gè)星期
a 上午 / 下午 標(biāo)記符
k 時(shí) 在一天中 (1~24)
K 時(shí) 在上午或下午 (0~11)
z 時(shí)區(qū)
*/
public class FormatDateTime {
public static void main(String[] args) {
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時(shí)mm分ss秒");
SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");
SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等價(jià)于now.toLocaleString()
SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH時(shí)mm分ss秒 E ");
SimpleDateFormat myFmt4=new SimpleDateFormat(
"一年中的第 D 天 一年中第w個(gè)星期 一月中第W個(gè)星期 在一天中k時(shí) z時(shí)區(qū)");
Date now=new Date();
System.out.println(myFmt.format(now));
System.out.println(myFmt1.format(now));
System.out.println(myFmt2.format(now));
System.out.println(myFmt3.format(now));
System.out.println(myFmt4.format(now));
System.out.println(now.toGMTString());
System.out.println(now.toLocaleString());
System.out.println(now.toString());
}
}
效果:
2004年12月16日 17時(shí)24分27秒 04/12/16 17:24 2004-12-16 17:24:27 2004年12月16日 17時(shí)24分27秒 星期四
一年中的第 351 天 一年中第51個(gè)星期 一月中第3個(gè)星期 在一天中17時(shí) CST時(shí)區(qū)
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004
下面是個(gè)JavaBean:
public class FormatDateTime {
public static String toLongDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時(shí)mm分ss秒 E ");
return myFmt.format(dt);
}
public static String toShortDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH時(shí)mm分");
return myFmt.format(dt);
}
public static String toLongTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");
return myFmt.format(dt);
}
public static String toShortTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");
return myFmt.format(dt);
}
public static void main(String[] args) {
Date now=new Date();
System.out.println(FormatDateTime.toLongDateString(now));
System.out.println(FormatDateTime.toShortDateString(now));
System.out.println(FormatDateTime.toLongTimeString(now));
System.out.println(FormatDateTime.toShortTimeString(now));
}
}
調(diào)用的main 測(cè)試結(jié)果:
2004年12月16日 17時(shí)38分26秒 星期四 04年12月16日 17時(shí)38分 17 38 26 0965 04/12/16 17:38
以上所述是小編給大家介紹的Java中SimpleDateFormat用法詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- java日期格式化SimpleDateFormat的使用詳解
- Java SimpleDateFormat中英文時(shí)間格式化轉(zhuǎn)換詳解
- Java中SimpleDateFormat的使用方法
- Java中SimpleDateFormat日期格式轉(zhuǎn)換詳解及代碼示例
- Java中SimpleDateFormat方法超詳細(xì)分析
- 淺析Java中SimpleDateFormat為什么是線(xiàn)程不安全的
- 解決Java中SimpleDateFormat線(xiàn)程不安全的五種方案
- Java中的SimpleDateFormat的線(xiàn)程安全問(wèn)題詳解
- java中SimpleDateFormat 的多線(xiàn)程安全問(wèn)題
相關(guān)文章
java實(shí)現(xiàn)簡(jiǎn)單猜數(shù)字游戲
這篇文章主要介紹了java實(shí)現(xiàn)簡(jiǎn)單猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
基于Springboot的漫畫(huà)網(wǎng)站平臺(tái)設(shè)計(jì)與實(shí)現(xiàn)
本文將基于Springboot實(shí)現(xiàn)開(kāi)發(fā)一個(gè)漫畫(huà)主題的網(wǎng)站,實(shí)現(xiàn)一個(gè)比漂亮的動(dòng)漫連載的網(wǎng)站系統(tǒng),界面設(shè)計(jì)優(yōu)雅大方,比較適合做畢業(yè)設(shè)計(jì)和課程設(shè)計(jì)使用,需要的可以參考一下2022-08-08
Java中基于推、拉模式的sentinel規(guī)則持久化詳解
這篇文章主要介紹了Java中基于推、拉模式的sentinel規(guī)則持久化詳解,推模式是sentinelDashboard?把規(guī)則推給Nacos,Nacos監(jiān)聽(tīng)規(guī)則的變化推給微服務(wù),拉模式是sentinelDashboard?把規(guī)則直接給微服務(wù),?Nacos定時(shí)的同步微服務(wù)端的規(guī)則,需要的朋友可以參考下2023-09-09
新版POI獲取日期類(lèi)型cell值過(guò)程圖解
這篇文章主要介紹了新版POI獲取日期類(lèi)型cell值過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
springboot的統(tǒng)一異常處理,使用@RestControllerAdvice詳解
@RestControllerAdvice是Spring Boot中的全局異常處理注解,結(jié)合了@ControllerAdvice和@ResponseBody的功能,通過(guò)創(chuàng)建自定義異常類(lèi)和全局異常處理器,可以實(shí)現(xiàn)統(tǒng)一異常處理,確保API的一致性和響應(yīng)的標(biāo)準(zhǔn)化2024-12-12
SpringBoot2.7.14整合redis7的詳細(xì)過(guò)程
這篇文章主要介紹了SpringBoot2.7.14整合redis7的詳細(xì)過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-10-10
spring使用@Async注解導(dǎo)致循環(huán)依賴(lài)問(wèn)題異常的排查記錄
這篇文章主要介紹了spring使用@Async注解導(dǎo)致循環(huán)依賴(lài)問(wèn)題異常的排查記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

