Android時間戳

將獲取的時間轉(zhuǎn)化成自己需要的格式還有轉(zhuǎn)化成昨天,今天,明天等格式,為了方便自己使用,不喜勿噴可飄過。

public class DateUtils {

/**

* 返回unix時間戳 (1970年至今的秒數(shù))

* @return

*/

public static long getUnixStamp(){

return System.currentTimeMillis()/1000;

}

/**

* 得到昨天的日期

* @return

*/

public static String getYestoryDate() {

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.DATE,-1);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String yestoday = sdf.format(calendar.getTime());

return yestoday;

}

/**

* 得到今天的日期

* @return

*/

public static? String getTodayDate(){

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String date = sdf.format(new Date());

return date;

}

/**

* 時間戳轉(zhuǎn)化為時間格式

* @param timeStamp

* @return

*/

public static String timeStampToStr(long timeStamp) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String date = sdf.format(timeStamp * 1000);

return date;

}

/**

* 得到日期?? yyyy-MM-dd

* @param timeStamp? 時間戳

* @return

*/

public static String formatDate(long timeStamp) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String date = sdf.format(timeStamp*1000);

return date;

}

/**

* 得到時間? HH:mm:ss

* @param timeStamp?? 時間戳

* @return

*/

public static String getTime(long timeStamp) {

String time = null;

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String date = sdf.format(timeStamp * 1000);

String[] split = date.split("\\s");

if ( split.length > 1 ){

time = split[1];

}

return time;

}

/**

* 將一個時間戳轉(zhuǎn)換成提示性時間字符串,如剛剛,1秒前

*

* @param timeStamp

* @return

*/

public static String convertTimeToFormat(long timeStamp) {

long curTime =System.currentTimeMillis() / (long) 1000 ;

long time = curTime - timeStamp;

if (time < 60 && time >= 0) {

return "剛剛";

} else if (time >= 60 && time < 3600) {

return time / 60 + "分鐘前";

} else if (time >= 3600 && time < 3600 * 24) {

return time / 3600 + "小時前";

} else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {

return time / 3600 / 24 + "天前";

} else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {

return time / 3600 / 24 / 30 + "個月前";

} else if (time >= 3600 * 24 * 30 * 12) {

return time / 3600 / 24 / 30 / 12 + "年前";

} else {

return "剛剛";

}

}

/**

* 將一個時間戳轉(zhuǎn)換成提示性時間字符串,(多少分鐘)

*

* @param timeStamp

* @return

*/

public static String timeStampToFormat(long timeStamp) {

long curTime =System.currentTimeMillis() / (long) 1000 ;

long time = curTime - timeStamp;

return time/60 + "";

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容