Java中實(shí)現(xiàn)時(shí)間類型轉(zhuǎn)換的代碼詳解
一、時(shí)間類型轉(zhuǎn)換代碼示例
示例代碼如下:
package com.lyp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.TimeZone; public class dateFormat { public static void main(String[] args) throws ParseException { //時(shí)間格式轉(zhuǎn)換 dateToString(); //時(shí)間類型轉(zhuǎn)換字符串 stringToDate(); //字符串轉(zhuǎn)換時(shí)間類型 cstToDate(); //CST格式字符串轉(zhuǎn)換時(shí)間類型 cstToGmt(); //CST時(shí)間類型轉(zhuǎn)換GMT格式字符串 gmtToString(); //GMT格式字符串轉(zhuǎn)換常規(guī)字符串 timeMillisToString(); //時(shí)間戳轉(zhuǎn)換字符串 stringToTimeMillis(); //字符串轉(zhuǎn)換時(shí)間戳 timeMillisToDate(); //時(shí)間戳轉(zhuǎn)化時(shí)間格式 dateToTimeMillis(); //時(shí)間格式轉(zhuǎn)化時(shí)間戳 } public static void dateToString() { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); String dateStr = dateFormat.format(date); System.out.println("時(shí)間類型轉(zhuǎn)換字符串:"+date+" --> "+dateStr); } public static void stringToDate() throws ParseException { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = "2022-10-14 15:08:33"; Date date = dateFormat.parse(dateStr); System.out.println("字符串轉(zhuǎn)換時(shí)間類型:"+dateStr+" --> "+date); } public static void cstToDate() throws ParseException { SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);//注意使用Locale.US參數(shù) String cstStr = "Tue May 16 19:30:20 CST 2022"; Date date = dateFormat.parse(cstStr); System.out.println("CST格式字符串轉(zhuǎn)換時(shí)間類型:"+cstStr+" --> "+date); } public static void cstToGmt() { Date date = new Date();//默認(rèn)為CST格式時(shí)間 SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'GMT+0800' yyyy", Locale.ENGLISH); String gmtStr = dateFormat.format(date); System.out.println("CST時(shí)間類型轉(zhuǎn)換GMT格式字符串: "+date+" --> "+gmtStr); } public static void gmtToString() throws ParseException { String gmtStr = "Sun Oct 2 00:00:00 GMT+0800 2022"; SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'GMT+0800' yyyy", Locale.ENGLISH); SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); Date date = dateFormat.parse(gmtStr); String dateStr = dateFormat2.format(date); System.out.println("GMT格式字符串轉(zhuǎn)換常規(guī)字符串:"+gmtStr+" --> "+date+" --> "+dateStr); } public static void timeMillisToString() { Long time = System.currentTimeMillis(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = dateFormat.format(new Date(time)); System.out.println("時(shí)間戳轉(zhuǎn)換字符串: "+time+" --> "+dateStr); } public static void stringToTimeMillis() throws ParseException { String dateStr = "2022-10-19 14:48:13"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Long time = dateFormat.parse(dateStr).getTime(); System.out.println("字符串轉(zhuǎn)換時(shí)間戳: "+dateStr+" --> "+time); } public static void timeMillisToDate() { Long time = System.currentTimeMillis(); Date date = new Date(time); System.out.println("時(shí)間戳轉(zhuǎn)換時(shí)間類型: "+time+" --> "+date); } public static void dateToTimeMillis() { Date date = new Date(); Long time = date.getTime(); System.out.println("時(shí)間格式轉(zhuǎn)化時(shí)間戳: "+date+" --> "+time); } }
運(yùn)行結(jié)果如下:
二、時(shí)間標(biāo)準(zhǔn)簡(jiǎn)介
簡(jiǎn)介參考鏈接:詳解Java中的時(shí)間處理與時(shí)間標(biāo)準(zhǔn)
1、UTC(世界標(biāo)準(zhǔn)時(shí)間)
協(xié)調(diào)世界時(shí),又稱世界標(biāo)準(zhǔn)時(shí)間或世界協(xié)調(diào)時(shí)間,簡(jiǎn)稱UTC(從英文“Coordinated Universal Time”/法文“TempsUniversel Coordonné”而來(lái)),是最主要的世界時(shí)間標(biāo)準(zhǔn),其以原子時(shí)秒長(zhǎng)為基礎(chǔ),在時(shí)刻上盡量接近于格林尼治標(biāo)準(zhǔn)時(shí)間。
2、GMT(格林尼治平時(shí))
格林尼治平時(shí)(又稱格林尼治平均時(shí)間或格林尼治標(biāo)準(zhǔn)時(shí)間,舊譯格林威治標(biāo)準(zhǔn)時(shí)間;英語(yǔ):Greenwich MeanTime,GMT)是指位于英國(guó)倫敦郊區(qū)的皇家格林尼治天文臺(tái)的標(biāo)準(zhǔn)時(shí)間,因?yàn)楸境踝游缇€被定義在通過(guò)那里的經(jīng)線。
理論上來(lái)說(shuō),格林尼治標(biāo)準(zhǔn)時(shí)間的正午是指當(dāng)太陽(yáng)橫穿格林尼治子午線時(shí)(也就是在格林尼治上空最高點(diǎn)時(shí))的時(shí)間。由于地球在它的橢圓軌道里的運(yùn)動(dòng)速度不均勻,這個(gè)時(shí)刻可能與實(shí)際的太陽(yáng)時(shí)有誤差,最大誤差達(dá)16分鐘。由于地球每天的自轉(zhuǎn)是有些不規(guī)則的,而且正在緩慢減速,因此格林尼治時(shí)間已經(jīng)不再被作為標(biāo)準(zhǔn)時(shí)間使用。現(xiàn)在的標(biāo)準(zhǔn)時(shí)間,是由原子鐘報(bào)時(shí)的協(xié)調(diào)世界時(shí)(UTC)。
3、CST(北京時(shí)間)
北京時(shí)間,China Standard Time,中國(guó)標(biāo)準(zhǔn)時(shí)間。在時(shí)區(qū)劃分上,屬東八區(qū),比協(xié)調(diào)世界時(shí)早8小時(shí),記為UTC+8。
不過(guò)這個(gè)CST這個(gè)縮寫(xiě)比較糾結(jié)的是它可以同時(shí)代表四個(gè)不同的時(shí)間: Central Standard Time (USA) UT-6:00Central Standard Time (Australia) UT+9:30 China Standard Time UT+8:00Cuba Standard Time UT-4:00
三、SimpleDateFormat 參數(shù)對(duì)照表
字母 | 時(shí)間 | 類型 | 示例 |
---|---|---|---|
G | Era 標(biāo)志符 | Text | AD |
y | 年份 | Number | 1996; 96 |
M | 年份中的月份 | Text | July; Jul; 07 |
w | 年份中的周數(shù) | Number | 27 |
W | 月份中的周數(shù) | Number | 2 |
D | 年份中的天數(shù) | Number | 189 |
d | 月份中的天數(shù) | Number | 10 |
F | 月份中的星期 | Number | 2 |
E | 星期中的天數(shù) | Text | Tuesday; Tue |
a | Am/pm 標(biāo)記 | Text | PM |
H | 一天中的小時(shí)數(shù)(0-23) | Number | 0 |
k | 一天中的小時(shí)數(shù)(1-24) | Number | 24 |
K | am/pm 中的小時(shí)數(shù)(0-11) | Number | 0 |
h | am/pm 中的小時(shí)數(shù)(1-12) | Number | 12 |
m | 小時(shí)中的分鐘數(shù) | Number | 30 |
s | 分鐘中的秒數(shù) | Number | 55 |
S | 毫秒數(shù) | Number | 978 |
z | 時(shí)區(qū) | General time zone | Pacific Standard Time; PST; GMT-08:00 |
Z | 時(shí)區(qū) | RFC 822 time zone | -0800 |
以上就是Java中實(shí)現(xiàn)時(shí)間類型轉(zhuǎn)換的代碼詳解的詳細(xì)內(nèi)容,更多關(guān)于Java時(shí)間類型轉(zhuǎn)換的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java?Unsafe創(chuàng)建對(duì)象的方法實(shí)現(xiàn)
Java中使用Unsafe實(shí)例化對(duì)象是一項(xiàng)十分有趣而且強(qiáng)大的功能,本文主要介紹了Java?Unsafe創(chuàng)建對(duì)象的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07oracle數(shù)據(jù)庫(kù)導(dǎo)入TXT文件方法介紹
這篇文章主要介紹了oracle數(shù)據(jù)庫(kù)導(dǎo)入TXT文件方法介紹,文中向大家展示了具體代碼示例,需要的朋友可以參考下。2017-09-09java實(shí)現(xiàn)微博后臺(tái)登錄發(fā)送微博
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微博后臺(tái)登錄發(fā)送微博的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07Java 3種方法實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換
這篇文章主要介紹了Java 3種方法實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換,幫助大家利用Java處理數(shù)據(jù),感興趣的朋友可以了解下2020-09-09SpringMVC的簡(jiǎn)單傳值(實(shí)現(xiàn)代碼)
下面小編就為大家?guī)?lái)一篇SpringMVC的簡(jiǎn)單傳值(實(shí)現(xiàn)代碼)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05