標(biāo)準(zhǔn)的時(shí)間一般長這樣子??
Wed Oct 19 2022 18:40:04 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間) {}
Wed Oct 19 2022 18:40:04這一坨 翻譯過來就是2022/10/19 18:41:13
那么GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間) {}表示啥呢?
GMT(格林尼治標(biāo)準(zhǔn)時(shí)間(英語:Greenwich Mean Time,GMT)又稱格林尼治平均時(shí)間)
+800表示中國位于東八區(qū)氯迂,想起了一部劇??
由于GMT計(jì)時(shí)每年有丟丟誤差兑凿,目前已經(jīng)被原子鐘報(bào)時(shí)的協(xié)調(diào)世界時(shí)(UTC)所取代
有興趣的可了解下夏令時(shí)
Epoch Time是計(jì)算從1970年1月1日零點(diǎn)(格林威治時(shí)區(qū)/GMT+00:00)到現(xiàn)在所經(jīng)歷的秒數(shù)
java中獲取現(xiàn)在時(shí)間戳
long l = System.currentTimeMillis();
Date ;java.util.Date;
Date date=new Date();
int year = date.getYear()+1900;
int month = date.getMonth()+1;
int date1 = date.getDate();
System.out.println(date.toString());
System.out.println(date.toLocaleString());
System.out.println(date.toGMTString());
//Wed Oct 19 19:11:34 CST 2022
//2022年10月19日 下午7:11:34
//19 Oct 2022 11:11:34 GMT
格式加工一下
Date date =new Date();
var sdf=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
sout(sdf.format(date))
Calendar
相較于Date骡澈,Calendar可以設(shè)置時(shí)區(qū)徽诲,做一些簡單日期加減
// 簡單例子
Calendar c=Calendar.getInstance();
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH)+1;
int date=c.get(Calendar.Date);
var sdf=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
sout(sdf.format(c.getTime()));//c.getTime()將Calendar轉(zhuǎn)為Date對象
// 自定義
Calendar c=Calendar.getInstance();
c.clear();
c.set(Calendar.YEAR,2022);
c.set(Calendar.MONTH,9);// 10月
c.set(Calendar.DATE,20);
//or 一把梭
c.set(2022,9,20,12,0,0);
System.out.println(new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(c.getTime()));
//2022-10-20 12:00:00
上面說了Calendar可以設(shè)置時(shí)區(qū)
TimeZone
// 初始化
TimeZone timeZone= TimeZone.getDefault();//系統(tǒng)默認(rèn)時(shí)區(qū)赤赊,有興趣打印一下
System.out.println(timeZone.getID());//Asia/Shanghai劝萤,亞洲上海
System.out.println(Arrays.deepToString(TimeZone.getAvailableIDs()));//所有可用時(shí)區(qū)
//[Africa/Abidjan, Africa/Accra, Africa/Addis_Ababa, Africa/Algiers, Africa/Asmara, Africa/Asmera, Africa/Bamako, Africa/Bangui, Africa/Banjul, Africa/Bissau, Africa/Blantyre, Africa/Brazzaville, Africa/Bujumbura.....]一長串.......??
//自定義
TimeZone timeZone=TimeZone.getTimeZone("America/Maceio");
//question 已知上海時(shí)間渊涝,求同一時(shí)刻America/Maceio的時(shí)間
Calendar c= Calendar.getInstance();
c.clear();
c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
c.set(2022,9,20,12,0,0);
var sdf =new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("America/Maceio"));
System.out.println(sdf.format(c.getTime()));
// 2022-10-20 01:00:00
// 簡單日期運(yùn)算
c.add(Calendar.DATE,5);
// 2022年10月25日 下午12:00:00