欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中實(shí)現(xiàn)時(shí)間類型轉(zhuǎn)換的代碼詳解

 更新時(shí)間:2023年09月18日 11:07:16   作者:愛(ài)打羽球的碼猿  
這篇文章主要為大家詳細(xì)介紹了Java中實(shí)現(xiàn)時(shí)間類型轉(zhuǎn)換的相關(guān)方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下

一、時(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í)間類型示例
GEra 標(biāo)志符TextAD
y年份Number1996; 96
M年份中的月份TextJuly; Jul; 07
w年份中的周數(shù)Number27
W月份中的周數(shù)Number2
D年份中的天數(shù)Number189
d月份中的天數(shù)Number10
F月份中的星期Number2
E星期中的天數(shù)TextTuesday; Tue
aAm/pm 標(biāo)記TextPM
H一天中的小時(shí)數(shù)(0-23)Number0
k一天中的小時(shí)數(shù)(1-24)Number24
Kam/pm 中的小時(shí)數(shù)(0-11)Number0
ham/pm 中的小時(shí)數(shù)(1-12)Number12
m小時(shí)中的分鐘數(shù)Number30
s分鐘中的秒數(shù)Number55
S毫秒數(shù)Number978
z時(shí)區(qū)General time zonePacific 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)文章

最新評(píng)論