我想做什么::
显示消息基于
- 早上好(中午 12 点至中午 12 点)
- 中午后(中午 12 点 -4 点)
- 晚上好(下午 4 点到晚上 9 点)
- 晚安(晚上 9 点到早上 6 点)
代码::
我使用 24 小时格式来获取此逻辑
private void getTimeFromAndroid() {
Date dt = new Date();
int hours = dt.getHours();
int min = dt.getMinutes();
if(hours>=1 || hours<=12){
Toast.makeText(this, "Good Morning", Toast.LENGTH_SHORT).show();
}else if(hours>=12 || hours<=16){
Toast.makeText(this, "Good Afternoon", Toast.LENGTH_SHORT).show();
}else if(hours>=16 || hours<=21){
Toast.makeText(this, "Good Evening", Toast.LENGTH_SHORT).show();
}else if(hours>=21 || hours<=24){
Toast.makeText(this, "Good Night", Toast.LENGTH_SHORT).show();
}
}
问题:
- 这是最好的方法吗,如果不是,那是最好的方法
原文由 Devrath 发布,翻译遵循 CC BY-SA 4.0 许可协议
你应该做这样的事情: