Show message based on
I would advise to use Java 8 LocalTime.
Maybe create a class like this to handle your time of day problem.
public class GreetingMaker { // think of a better name then this.
private static final LocalTime MORNING = LocalTime.of(0, 0, 0);
private static final LocalTime AFTER_NOON = LocalTime.of(12, 0, 0);
private static final LocalTime EVENING = LocalTime.of(16, 0, 0);
private static final LocalTime NIGHT = LocalTime.of(21, 0, 0);
private LocalTime now;
public GreetingMaker(LocalTime now) {
this.now = now;
}
public void printTimeOfDay() { // or return String in your case
if (between(MORNING, AFTER_NOON)) {
System.out.println("Good Morning");
} else if (between(AFTER_NOON, EVENING)) {
System.out.println("Good Afternoon");
} else if (between(EVENING, NIGHT)) {
System.out.println("Good Evening");
} else {
System.out.println("Good Night");
}
}
private boolean between(LocalTime start, LocalTime end) {
return (!now.isBefore(start)) && now.isBefore(end);
}
}
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.
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;
}
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");
}
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();
}
More specific
public static String getDayMessage() {
Calendar c = Calendar.getInstance();
int timeOfDay = c.get(Calendar.HOUR_OF_DAY);
if (timeOfDay < 5) {
return "Hi, Good Mid Night";
}else if (timeOfDay < 6) {
return "Hi, Good Late Night";
} else if (timeOfDay < 12) {
return "Hi, Good Morning";
} else if (timeOfDay < 14) {
return "Hi, Good Noon";
} else if (timeOfDay < 16) {
return "Hi, Good Afternoon";
} else if (timeOfDay < 21) {
return "Hi, Good Evening";
} else {
return "Hi, Good Night";
}
}