我想做什么::
显示消息基于
早上好(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(); } }
题:
解决方法
你应该这样做:
Calendar c = Calendar.getInstance(); int timeOfDay = c.get(Calendar.HOUR_OF_DAY); if(timeOfDay >= 0 && timeOfDay < 12){ Toast.makeText(this,Toast.LENGTH_SHORT).show(); }else if(timeOfDay >= 12 && timeOfDay < 16){ Toast.makeText(this,Toast.LENGTH_SHORT).show(); }else if(timeOfDay >= 16 && timeOfDay < 21){ Toast.makeText(this,Toast.LENGTH_SHORT).show(); }else if(timeOfDay >= 21 && timeOfDay < 24){ Toast.makeText(this,Toast.LENGTH_SHORT).show(); }