- 将秒数格式化为字符串
/**
* 将秒数格式化为字符串
*
* @param secs 秒 secFull 分or分钟
*
* @return %d天%d小时%d分钟
*/
public static String formatTimeString(int secs, boolean secFull) {
String strDay = "";
String strHour = "";
String strSec = "";
int iDay = 0;
int iHour = 0;
int iSec = 0;
int time = secs;
iDay = time / 86400;
if (iDay > 0) {
time = time % 86400;
}
iHour = time / 3600;
if (iHour > 0) {
time = time % 3600;
}
iSec = time / 60;
time = time % 60;
if (time > 0 && time >= 30) {
iSec += 1;
}
if (iDay == 0 && iHour == 0 && iSec == 0) {
// 时间都为零时,强制为1分钟
iSec = 1;
}
if (iDay > 0) {
// 天数有效时,如果分钟大于30则小时+1,否则不展示分钟
if (iSec >= 30) {
iHour += 1;
} else {
iSec = 0;
}
strDay = String.format("%d天", iDay); // 天
}
if (iHour > 0) {
if (iSec > 0) {
strHour = String.format("%d时", iHour); // 时
} else {
strHour = String.format("%d小时", iHour); // 小时
}
}
if (iDay <= 0 && iSec > 0) {
if (iHour > 0) {
strSec = String.format("%d分", iSec); // 分钟
} else {
strSec = String.format("%d分钟", iSec); // 分钟
}
}
return strDay + strHour + strSec;
}
- 判断时间戳是今天时间
public static boolean isToday(Calendar calendar) {
Calendar mCalendarNow = Calendar.getInstance();
return mCalendarNow.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)
&& mCalendarNow.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)
&& mCalendarNow.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH);
}
-
判断时间戳是明天
public static boolean isTomorrow(Calendar calendar) {
Calendar mCalendarNow = Calendar.getInstance();
mCalendarNow.add(Calendar.DATE, 1);
return mCalendarNow.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)
&& mCalendarNow.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)
&& mCalendarNow.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH);
} -
判断时间戳是后天
public static boolean isAfter(Calendar calendar) {
Calendar mCalendarNow = Calendar.getInstance();
mCalendarNow.add(Calendar.DATE, 2);
return mCalendarNow.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)
&& mCalendarNow.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)
&& mCalendarNow.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH);
} -
获取年份差
/**
* 计算年数之差
*
*/
public static int getElapsedYear(Date fromDate, Date toDate) {
int year = -1;
try {
Calendar from = Calendar.getInstance();
from.setTime(fromDate);
Calendar to = Calendar.getInstance();
to.setTime(toDate);
int fromYear = from.get(Calendar.YEAR);
int toYear = to.get(Calendar.YEAR);
if (fromYear < toYear) {
year = -1;
} else {
year = toYear - fromYear;
}
} catch (Exception e) {
// nothing todo
}
return year;
}
- 获取小时差
public static int getElapsedHours(Date fromDate, Date toDate) {
int hours = -1;
try {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
String from = sf.format(fromDate);
String to = sf.format(toDate);
long fromMS = sf.parse(from).getTime();
long toMS = sf.parse(to).getTime();
if (fromMS > toMS) {
hours = -1;
} else {
hours = (int) ((toMS - fromMS) / (1000 * 60 * 60));
}
} catch (Exception e) {
ExceptionHelpUtil.addExceptionMessageOffline(e);
}
return hours;
}
网友评论