计算通话结束时距离现在过了多久
String strTime = ""; Date callDate = callRecord.getCallDate();//通话结束时时间 long callTime = callDate.getTime();//返回通话结束时时间毫秒值 long currentTimeMillis = System.currentTimeMillis();//当前时间毫秒值 // 小于60分钟 if ((currentTimeMillis - callTime) / 1000 / 60 < 60) { // 小于1分钟 if (((currentTimeMillis - callTime) / 1000 / 60) == 0) { strTime = "刚刚"; } else { strTime = ((currentTimeMillis - callTime) / 1000 / 60) + "分钟前"; } } else if ((currentTimeMillis - callTime) / 1000 / 60 > 60 && ((currentTimeMillis - callTime) / 1000 / 60) < (24 * 60)) { // 大于60分钟且小于24小时 strTime = ((currentTimeMillis - callTime) / 1000 / 60 / 60) + "小时前"; } else if (((currentTimeMillis - callTime) / 1000 / 60) > (24 * 60) && ((currentTimeMillis - callTime) / 1000 / 60) < (24 * 60 * 7)) { // 大于24小时且小于7天 strTime = ((currentTimeMillis - callTime) / 1000 / 60 / 60 / 24) + "天前"; } else if (((currentTimeMillis - callTime) / 1000 / 60) >= (24 * 60 * 7)) { // 大于7天,显示具体时间日期 strTime = new SimpleDateFormat("MM-dd HH:mm").format(callDate); } viewHolder.calls_list_callDate.setText(strTime);
倒计时
可显示天时分秒的倒计时
import android.os.CountDownTimer; /** * * @param millisecond:毫秒值 * @param timeText:显示倒计时的TextView控件 */ public static void remainingTime(int millisecond, TextView timeText) { CountDownTimer countDownTimer = new CountDownTimer(millisecond, 1000) { @Override public void onTick(long millisUntilFinished) { ChatUtil.getIns().setPromotionSurplusMillisecond(millisUntilFinished); long day = millisUntilFinished / (1000 * 60 * 60 * 24); long hour = (millisUntilFinished - day * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); long minute = (millisUntilFinished - day * (1000 * 60 * 60 * 24) - hour * (1000 * 60 * 60)) / (1000 * 60); long second = (millisUntilFinished - day * (1000 * 60 * 60 * 24) - hour * (1000 * 60 * 60) - minute * (1000 * 60)) / 1000; String hourStr = hour + ""; String minuteStr = minute + ""; String secondStr = second + ""; if (hour / 10 < 1) { hourStr = 0 + hourStr; } if (minute / 10 < 1) { minuteStr = 0 + minuteStr; } if (second / 10 < 1) { secondStr = 0 + secondStr; } if (timeText != null) { timeText.setText(hourStr + ":" + minuteStr + ":" + secondStr); } } @Override public void onFinish() { // 倒计时为0时 if (timeText!= null) { timeText.setVisibility(View.GONE); } } }; countDownTimer.start(); }
只显示分秒的倒计时
/** * @param millisecond:毫秒数 */ private void remainingTime(int millisecond) { CountDownTimer countDownTimer = new CountDownTimer(millisecond, 1000) { @Override public void onTick(long millisUntilFinished) { long minute = millisUntilFinished / (1000 * 60);//分 long second = (millisUntilFinished - minute * (1000 * 60)) / 1000;//秒 String minuteStr = minute + ""; String secondStr = second + ""; if (minute / 10 < 1) { minuteStr = 0 + minuteStr; } if (second / 10 < 1) { secondStr = 0 + secondStr; } await_receive_time.setText(minuteStr + ":" + secondStr); } @Override public void onFinish() { // 倒计时为0时进入这个方法 effect_get_button.setEnabled(true); effect_red_dot.setVisibility(View.VISIBLE); await_receive_time_img.setVisibility(View.GONE); await_receive_time.setVisibility(View.GONE); } }; countDownTimer.start(); }