- 相關(guān)推薦
Java相對(duì)時(shí)間轉(zhuǎn)換代碼
導(dǎo)語(yǔ):Java如何實(shí)現(xiàn)時(shí)間的轉(zhuǎn)換呢?下面是小編給大家提供的Java相對(duì)時(shí)間轉(zhuǎn)換代碼,大家可以參考閱讀,更多詳情請(qǐng)關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。
private String dateToRelativeTime(Date date){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now=new Date();
long between=(now.getTime()-date.getTime())/1000;//除以1000是為了轉(zhuǎn)換成秒
long day=between/(24*3600);
long hour=between%(24*3600)/3600;
long minute=between%3600/60;
long second=between;
String timeStr;
if(day>0&&day<=29){
timeStr=day+"天前";
}else if(day>29){
timeStr=df.format(date);
}else{
if(hour>0){
timeStr=hour+"小時(shí)前";
}else{
if(minute>0){
timeStr=minute+"分鐘前";
}else{
timeStr=second+"秒前";
}
}
}
return timeStr;
}