TimeUtils.java
private static long lastClickTime = 0;
/**
* 判断两次点击的时间间隔是否过小
* @param time
* @return
*/
public static boolean isFastClick(long time) {
try {
long clickTime = SystemClock.elapsedRealtime();// System.currentTimeMillis();
if ((clickTime - lastClickTime) > time) {
lastClickTime = clickTime;
return false;
}
} catch (Exception e) {
}
return true;
}
/**
* 获取显示日期的内容
* @param timeMillis
* @return
*/
public static String getCustomTime(long timeMillis) {
String customTime = "";
int createDate[] = getCreateDate(timeMillis);
int currentDate[] = getCurrentDate();
SimpleDateFormat sDateFormat;
if (currentDate[0] - createDate[0] > 0) {//不同年
sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
} else if (currentDate[1] - createDate[1] > 0) {//同年 不同月
sDateFormat = new SimpleDateFormat("MM-dd HH:mm");
} else if (currentDate[2] - createDate[2] > 0) {//同年 同月 不同日
sDateFormat = new SimpleDateFormat("MM-dd HH:mm");
} else if (currentDate[3] - createDate[3] > 0) {//同年 同月 同日 不同时
sDateFormat = new SimpleDateFormat("HH:mm");
} else {
sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
}
customTime = sDateFormat.format(new Date(timeMillis));
return customTime;
}
public static int[] getCreateDate(long timeMillis) {
int[] date = new int[6];//年、月、日、时、分、秒
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeMillis);
//输出年
//System.out.println(calendar.get(Calendar.YEAR));
//输出月 :由于月份是从0开始的所以要加上1
//System.out.println(calendar.get(Calendar.MONTH) + 1);
//输出日
//System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
date[0] = calendar.get(Calendar.YEAR);
date[1] = calendar.get(Calendar.MONTH) + 1;
date[2] = calendar.get(Calendar.DAY_OF_MONTH);
date[3] = calendar.get(Calendar.HOUR_OF_DAY);
date[4] = calendar.get(Calendar.MINUTE);
date[5] = calendar.get(Calendar.MILLISECOND);
return date;
}
public static int[] getCurrentDate() {
int[] date = new int[6];//年、月、日、时、分、秒
Date today = new Date();
long time = today.getTime();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
//输出年
//System.out.println(calendar.get(Calendar.YEAR));
//输出月 :由于月份是从0开始的所以要加上1
//System.out.println(calendar.get(Calendar.MONTH) + 1);
//输出日
//System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
date[0] = calendar.get(Calendar.YEAR);
date[1] = calendar.get(Calendar.MONTH) + 1;
date[2] = calendar.get(Calendar.DAY_OF_MONTH);
date[3] = calendar.get(Calendar.HOUR_OF_DAY);
date[4] = calendar.get(Calendar.MINUTE);
date[5] = calendar.get(Calendar.MILLISECOND);
return date;
}