Java的Date格式的應用

2021-07-25 12:02:10 字數 1456 閱讀 6165

/**

* 從秒數獲得乙個具體時間,24小時制,比如2016-12-12 23:23:15

* @param second

* @return

*/public static string second2moment24(long second)

/** * 從秒數獲得乙個具體時間,12小時制,比如2016-12-12 23:23:15

* @param second

* @return

*/public static string second2moment12(long second)

/** * 從秒數獲得乙個具體時間,24小時制,比如2016-12-12 23:23:15

* @param second

* @return

*/public static string second2momentgmt0(long second)

/** * 從乙個具體時間,比如2016-12-12 23:23:15,獲得秒數

* @param time

* @return

*/public static long gettimestamp(string time)

catch (parseexception e)

long timestemp = date.gettime() / 1000;

return timestemp;

}

1、為什麼從system.currenttimemillis()轉成標準時間格式會相差12個小時?

比如當前時間為2016-12-19 21:36:20,呼叫second2moment12(system.currenttimemillis()/1000),輸出的是2016-12-19 09:36:20.

原因是弄混了12小時制和24小時制,******dateformat應該使用"yyyy-mm-dd hh:mm:ss"

2、為什麼從system.currenttimemillis()轉成標準時間格式會相差8個小時?

比如當前時間為2016-12-19 21:36:20,呼叫second2momentgmt0(system.currenttimemillis()/1000),輸出的是2016-12-19 13:36:20.

用system.currenttimemillis()獲取系統時間是國際標準的時間,是0時區的時間。原因有可能是******dateformat沒有設定時區,ft.settimezone(timezone.gettimezone("gmt+8"))。

3、如何從乙個時間字串比如2016-12-19 13:36:20獲得其秒數,可以用來比較時間的早晚

比如當前時間為2016-12-19 21:36:20,呼叫gettimestamp("2016-12-19 21:36:20"),輸出的是1482154580.

Java中日期Date的格式化輸出

mydate為要輸出的日期,fromatstring為輸入的格式,如 yyyy mm dd 輸出為 2010 04 14 public static string dateformat date mydate,string fromatstring 另貼出 輸出日期格式引數的對應表 字母日期或時間元...

js的date日期格式

有時候做專案會用到js的date日期格式,因為date 返回的格式不是我們需要的,date 返回格式 thu mar 19 2015 12 00 00 gmt 0800 中國標準時間 而我們則需要這樣的格式 2015 3 19 12 00 00 除非是在後台處理好時間格式,然後在頁面直接顯示。那如何...

修改git log中的Date格式

預設的git log檢視日誌顯示的格式如下 date thu aug 16 17 44 32 2018 0800 說實話,真不太喜歡這種日期格式還是換成數值比較舒服一點。git bash中使用命令設定即可 修改當前倉庫log date格式 git config log.date iso strict...