Showing Morning, afternoon, evening, night message

2019-02-04 10:08发布

What i am trying to do::

Show message based on

  • Good morning (12am-12pm)
  • Good after noon (12pm -4pm)
  • Good evening (4pm to 9pm)
  • Good night ( 9pm to 6am)

CODE::

I used 24-hr format to get this logic

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();
        }
    }

Question:

  • Is this this best way of doing it, If no which is the best way

9条回答
Deceive 欺骗
2楼-- · 2019-02-04 10:13

I would shorten your if/elseif statement to:

String greeting = null;
if(hours>=1 && hours<=12){
    greeting = "Good Morning";
} else if(hours>=12 && hours<=16){
    greeting = "Good Afternoon";
} else if(hours>=16 && hours<=21){
    greeting = "Good Evening";
} else if(hours>=21 && hours<=24){
    greeting = "Good Night";
}
Toast.makeText(this, greeting, Toast.LENGTH_SHORT).show();
查看更多
Emotional °昔
3楼-- · 2019-02-04 10:15

When I write

Calendar c = Calendar.getInstance();
int timeOfDay = c.get(Calendar.HOUR_OF_DAY);

I didn't get output and it doesn't show any error. Just the timeOfDay won't get assigned any value in the code. I felt it was because of some threading while Calendar.getInstance() is executed. But when I collapsed the lines it worked for me. See the following code:

int timeOfDay = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

if(timeOfDay >= 0 && timeOfDay < 12){
        greeting.setText("Good Morning");
}else if(timeOfDay >= 12 && timeOfDay < 16){
        greeting.setText("Good Afternoon");
}else if(timeOfDay >= 16 && timeOfDay < 21){
        greeting.setText("Good Evening");
}else if(timeOfDay >= 21 && timeOfDay < 24){
        greeting.setText("Good Morning");
}
查看更多
倾城 Initia
4楼-- · 2019-02-04 10:17

You should be doing something like:

Calendar c = Calendar.getInstance();
int timeOfDay = c.get(Calendar.HOUR_OF_DAY);

if(timeOfDay >= 0 && timeOfDay < 12){
    Toast.makeText(this, "Good Morning", Toast.LENGTH_SHORT).show();        
}else if(timeOfDay >= 12 && timeOfDay < 16){
    Toast.makeText(this, "Good Afternoon", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 16 && timeOfDay < 21){
    Toast.makeText(this, "Good Evening", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 21 && timeOfDay < 24){
    Toast.makeText(this, "Good Night", Toast.LENGTH_SHORT).show();
}
查看更多
不美不萌又怎样
5楼-- · 2019-02-04 10:19

Using Time4J (or Time4A on Android) enables following solutions which do not need any if-else-statements:

ChronoFormatter<PlainTime> parser =
    ChronoFormatter.ofTimePattern("hh:mm a", PatternType.CLDR, Locale.ENGLISH);
PlainTime time = parser.parse("10:05 AM");

Map<PlainTime, String> table = new HashMap<>();
table.put(PlainTime.of(1), "Good Morning");
table.put(PlainTime.of(12), "Good Afternoon");
table.put(PlainTime.of(16), "Good Evening");
table.put(PlainTime.of(21), "Good Night");
ChronoFormatter<PlainTime> customPrinter=
    ChronoFormatter
      .setUp(PlainTime.axis(), Locale.ENGLISH)
      .addDayPeriod(table)
      .build();
System.out.println(customPrinter.format(time)); // Good Morning

There is also another pattern-based way to let the locale decide in a standard way based on CLDR-data how to format the clock time:

ChronoFormatter<PlainTime> parser =
    ChronoFormatter.ofTimePattern("hh:mm a", PatternType.CLDR, Locale.ENGLISH);
PlainTime time = parser.parse("10:05 AM");

ChronoFormatter<PlainTime> printer1 =
    ChronoFormatter.ofTimePattern("hh:mm B", PatternType.CLDR, Locale.ENGLISH);
System.out.println(printer1.format(time)); // 10:05 in the morning

ChronoFormatter<PlainTime> printer2 =
    ChronoFormatter.ofTimePattern("B", PatternType.CLDR, Locale.ENGLISH)
        .with(Attributes.OUTPUT_CONTEXT, OutputContext.STANDALONE);
System.out.println(printer2.format(time)); // morning

The only other library known to me which can also do this (but in an awkward way) is ICU4J.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-02-04 10:20
 private String getStringFromMilli(long millis) {

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(millis);
    int hours = c.get(Calendar.HOUR_OF_DAY);

    if(hours >= 1 && hours <= 12){
        return "MORNING";
    }else if(hours >= 12 && hours <= 16){
        return "AFTERNOON";
    }else if(hours >= 16 && hours <= 21){
        return "EVENING";
    }else if(hours >= 21 && hours <= 24){
        return "NIGHT";
    }
    return null;
}
查看更多
不美不萌又怎样
7楼-- · 2019-02-04 10:29

try this code(get hours and get minute methods in Date class are deprecated.)

 private void getTimeFromAndroid() {
    Date dt = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(dt);
    int hours = c.get(Calendar.HOUR_OF_DAY);
    int min = c.get(Calendar.MINUTE);

    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();
    }
}
查看更多
登录 后发表回答