1.这个方法愿意的话就自己研究一下,直接复制走就可以了。(非原创)
即使机主的手机不是24小时制的一样有效,如(上午、下午这种格式的时间一样是有效的)
原文链接:https://my.oschina.net/colorchen789/blog/3003644
`/**
* 判断当前系统时间是否在指定时间的范围内
*
* beginHour 开始小时,例如22
* beginMin 开始小时的分钟数,例如30
* endHour 结束小时,例如 8
* endMin 结束小时的分钟数,例如0
* true表示在范围内, 否则false
*/
public static boolean isCurrentInTimeScope(int beginHour, int beginMin, int endHour, int endMin) {
boolean result = false;
final long aDayInMillis = 1000 * 60 * 60 * 24;
final long currentTimeMillis = System.currentTimeMillis();
Time now = new Time();
now.set(currentTimeMillis);
Time startTime = new Time();
startTime.set(currentTimeMillis);
startTime.hour = beginHour;
startTime.minute = beginMin;
Time endTime = new Time();
endTime.set(currentTimeMillis);
endTime.hour = endHour;
endTime.minute = endMin;
// 跨天的特殊情况(比如22:00-8:00)
if (!startTime.before(endTime)) {
startTime.set(startTime.toMillis(true) - aDayInMillis);
result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime
Time startTimeInThisDay = new Time();
startTimeInThisDay.set(startTime.toMillis(true) + aDayInMillis);
if (!now.before(startTimeInThisDay)) {
result = true;
}
} else {
//普通情况(比如 8:00 - 14:00)
result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime
}
return result;
}`
2.调用判断是否在某个时间段,如8:00直接写8即可,后边的分写0即可。
if (isCurrentInTimeScope(0,0,8,0)){
mTextView.setText("早上好");
}
if (isCurrentInTimeScope(8,0,12,0)){
mTextView.setText("上午好");
}
if (isCurrentInTimeScope(12,0,14,0)){
mTextView.setText("中午好");
}
if (isCurrentInTimeScope(14,0,18,0)){
mTextView.setText("下午好");
}
if (isCurrentInTimeScope(18,0,24,0)){
mTextView.setText("晚上好");
}