在線時間戳轉(zhuǎn)換工具
在java中獲取時間戳方式:
//獲取現(xiàn)在的秒級別的時間戳
long time = System.currentTimeMillis() / 1000;
//獲取現(xiàn)在的毫秒級別的時間戳
long time = System.currentTimeMillis();
下面方法中,在調(diào)用Date構(gòu)造函數(shù)而不傳遞參數(shù)的情況下,新創(chuàng)建的對象自動獲得當(dāng)前日期和時間。如果想傳入特定日期,需將表示日期的字符串傳遞給Date構(gòu)造函數(shù)。
//時間戳轉(zhuǎn)為時間格式y(tǒng)yyy-MM-dd HH:mm:ss
function getDateTimeToDate(a) {
//這里接收的是一個String類型的數(shù)值
var now;
//當(dāng)時間戳小于1000000000000時代表是秒級別的時間戳
if (parseInt(a) < 1000000000000) {
now = new Date(parseInt(a) * 1000);
} else {
now = new Date(parseInt(a));
}
var year = now.getFullYear() < 10 ? '0' + now.getFullYear() : now.getFullYear(); //取得4位數(shù)的年份
var month = now.getMonth() + 1 < 10 ? '0' + (now.getMonth() + 1) : now.getMonth() + 1; //取得日期中的月份,其中0表示1月,11表示12月
var date = now.getDate() < 10 ? '0' + now.getDate() : now.getDate(); //返回日期月份中的天數(shù)(1到31)
var hour = now.getHours() < 10 ? '0' + now.getHours() : now.getHours(); //返回日期中的小時數(shù)(0到23)
var minute = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();//返回日期中的分鐘數(shù)(0到59)
var second = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds();//返回日期中的秒數(shù)(0到59)
return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}