java前幾分鐘的代碼 java時(shí)間分段

用java如何實(shí)現(xiàn) 1小時(shí)前 1分鐘前

/**

為牧野等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及牧野網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、牧野網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

* 顯示時(shí)間,如果與當(dāng)前時(shí)間差別小于一天,則自動(dòng)用**秒(分,小時(shí))前,如果大于一天則用format規(guī)定的格式顯示

*

* @author wxy

* @param ctime

* 時(shí)間

* @param format

* 格式 格式描述:例如:yyyy-MM-dd yyyy-MM-dd HH:mm:ss

* @return

*/

public static String showTime(Date ctime, String format) {

String r = "";

if(ctime==null)return r;

if(format==null)format="yyyy-MM-dd HH:mm";

long nowtimelong = System.currentTimeMillis();

long ctimelong = ctime.getTime();

long result = Math.abs(nowtimelong - ctimelong);

if (result 60000)// 一分鐘內(nèi)

{

long seconds = result / 1000;

r = seconds + "秒鐘前";

} else if (result = 60000 result 3600000)// 一小時(shí)內(nèi)

{

long seconds = result / 60000;

r = seconds + "分鐘前";

} else if (result = 3600000 result 86400000)// 一天內(nèi)

{

long seconds = result / 3600000;

r = seconds + "小時(shí)前";

} else// 日期格式

{

r = DateTime.formatTime(ctime, format);

}

return r;

}

如何用java 獲取15分鐘以前的時(shí)間

代碼如下:

1.得到當(dāng)前的時(shí)間

Calendar cal = Calendar.getInstance();

String time=formatter.format(cal.getTime());

String strEnd=time.substring(0, 16);// 2007-10-30 09:30

2.得到前一天的時(shí)間

Calendar c = Calendar.getInstance();

c.add(Calendar.DAY_OF_MONTH, -1);

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

String mDateTime=formatter.format(c.getTime());

String strStart=mDateTime.substring(0, 16);//2007-10-29 09:30

3.得到前幾天的時(shí)間

Calendar c = Calendar.getInstance();

c.add(Calendar.DAY_OF_MONTH, -5);//得到前5

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

String mDateTime=formatter.format(c.getTime());

String strStart=mDateTime.substring(0, 16);//2007-10-24 09:30

4.得到當(dāng)前時(shí)間的前幾年的時(shí)間

Calendar c = Calendar.getInstance();

c.add(Calendar.YEAR, -5);//得到前5年

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

String mDateTime=formatter.format(c.getTime());

String strStart=mDateTime.substring(0, 16);//2002-10-24 09:30

java8判斷幾秒前,幾分鐘前,幾天前

看下對(duì)你是否有用

public?class?RelativeDateFormat?{

private?static?final?long?ONE_MINUTE?=?60000L;

private?static?final?long?ONE_HOUR?=?3600000L;

private?static?final?long?ONE_DAY?=?86400000L;

private?static?final?long?ONE_WEEK?=?604800000L;

private?static?final?String?ONE_SECOND_AGO?=?"秒前";

private?static?final?String?ONE_MINUTE_AGO?=?"分鐘前";

private?static?final?String?ONE_HOUR_AGO?=?"小時(shí)前";

private?static?final?String?ONE_DAY_AGO?=?"天前";

private?static?final?String?ONE_MONTH_AGO?=?"月前";

private?static?final?String?ONE_YEAR_AGO?=?"年前";

public?static?void?main(String[]?args)?throws?ParseException?{

SimpleDateFormat?format?=?new?SimpleDateFormat("yyyy-MM-dd?HH:m:s");

Date?date?=?format.parse("2013-11-11?18:35:35");

System.out.println(format(date));

}

public?static?String?format(Date?date)?{

long?delta?=?new?Date().getTime()?-?date.getTime();

if?(delta??1L?*?ONE_MINUTE)?{

long?seconds?=?toSeconds(delta);

return?(seconds?=?0???1?:?seconds)?+?ONE_SECOND_AGO;

}

if?(delta??45L?*?ONE_MINUTE)?{

long?minutes?=?toMinutes(delta);

return?(minutes?=?0???1?:?minutes)?+?ONE_MINUTE_AGO;

}

if?(delta??24L?*?ONE_HOUR)?{

long?hours?=?toHours(delta);

return?(hours?=?0???1?:?hours)?+?ONE_HOUR_AGO;

}

if?(delta??48L?*?ONE_HOUR)?{

return?"昨天";

}

if?(delta??30L?*?ONE_DAY)?{

long?days?=?toDays(delta);

return?(days?=?0???1?:?days)?+?ONE_DAY_AGO;

}

if?(delta??12L?*?4L?*?ONE_WEEK)?{

long?months?=?toMonths(delta);

return?(months?=?0???1?:?months)?+?ONE_MONTH_AGO;

}?else?{

long?years?=?toYears(delta);

return?(years?=?0???1?:?years)?+?ONE_YEAR_AGO;

}

}

private?static?long?toSeconds(long?date)?{

return?date?/?1000L;

}

private?static?long?toMinutes(long?date)?{

return?toSeconds(date)?/?60L;

}

private?static?long?toHours(long?date)?{

return?toMinutes(date)?/?60L;

}

private?static?long?toDays(long?date)?{

return?toHours(date)?/?24L;

}

private?static?long?toMonths(long?date)?{

return?toDays(date)?/?30L;

}

private?static?long?toYears(long?date)?{

return?toMonths(date)?/?365L;

}

}

文章標(biāo)題:java前幾分鐘的代碼 java時(shí)間分段
網(wǎng)站URL:http://muchs.cn/article32/dohhjpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站營銷虛擬主機(jī)、手機(jī)網(wǎng)站建設(shè)Google、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)