What times (hours) of day do you consider to be “morning”, afternoon”, “evening”, “night”, etc?
What time is "afternoon" and when is "evening" ?
java - Reading Time from String into "Morning", "Afternoon", "Evening", "night" - Stack Overflow
Routine Chart
Videos
As the title says. My daughter and I were having a conversation earlier, and she asked me to order something from Amazon; I was busy so I told her “please remind me this afternoon”. She responded by telling me it is already afternoon (it was 12:10pm). So this made me start to think about times of day and if there is a standard, so I googled it and that was useless because it was kind of all over the place or not specific enough… no real standard definition that I could find.
I would like to preface this by saying this is how I personally reference the different “time periods” throughout the day, it has nothing to do with any proper definitions or scientific research, this is just how I, myself, will reference the different time periods throughout the day :)
So I’m thinking maybe everyone kinda has their own personal “range” they use for specific times of the day? Anyway, I thought it would be fun to see what hours everyone else uses/considers to be morning/noon/afternoon/evening/night/early morning etc or whatever … so here’s mine:
Ok, so to me… (and this is just how I personally define the times of day, when I’m speaking about morning/noon/night etc) goes kind of like this:
morning is like 5am-12pm noon, noon is like 11am-1pm, afternoon is anytime between 1-5pm, evening is between 5-9pm, nighttime is after 9pm til like 2am, then it’s early morning from like 2-5am. So, for example, if it’s like 11am, and I ask my daughter “will you please remind me this afternoon”, I usually mean sometime between 1-3pm, but if I say “will you please remind me later this afternoon” that usually means anytime between 3-5ish pm.
am I psycho? Or does everyone have like a set period of time (in hours) that they kind of use to describe the times of day?
TLDR: What hours of the day do you consider when referencing the different time periods throughout a 24 hour period? For example: Morning/Noon/Afternoon/Evening/Night/Late Night/Early Morning
Here's one solution:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm");
LocalTime time = LocalTime.parse(input, fmt);
int d = 100*time.getHour() + time.getMinute();
if (d <= 400 || d > 1830) {
System.out.println("Night");
} else if (d <= 1100) {
System.out.println("Morning");
} else if (d <= 1500) {
System.out.println("Afternoon");
} else {
System.out.println("Evening");
}
But you don't need to convert the input string into a date/time.
Which part im doing wrong?
- You are trying to compare objects using operators for primitives
- Your pattern for parsing the time of day should be
HH:mminstead ofhh:mm
Alternative: java.time
Since this is all about the time of day disregarding day, month, year (and even seconds or smaller units), you can use a class designed for exactly that, a java.time.LocalTime.
Here's a readable example:
/**
* Determines the prosaic daytime (Morining, Afternoon, Evening, Night) of
* the time of day passed as {@code String}.
*
* @param timeOfDay time of day in 24h format
* @return prosaic daytime
*/
public static String dayTime(String timeOfDay) {
// parse the input without applying a formatter
LocalTime localTime = LocalTime.parse(timeOfDay);
// define the border values
LocalTime eleven = LocalTime.of(11, 0);
LocalTime four = LocalTime.of(4, 0);
LocalTime fifteen = LocalTime.of(15, 0);
LocalTime eighteenThirty = LocalTime.of(18, 30);
// check if the time is after four and either before or exactly eleven
if (localTime.isAfter(four) &&
(localTime.isBefore(eleven) || localTime.equals(eleven)))
return "Morning";
// check if the time is after eleven and either before or exactly fifteen
else if (localTime.isAfter(eleven) &&
(localTime.isBefore(fifteen) || localTime.equals(fifteen)))
return "Afternoon";
// check if the time is after fifteen and either before or exactly eighteen thirty
else if (localTime.isAfter(fifteen) &&
(localTime.isBefore(eighteenThirty) || localTime.equals(eighteenThirty)))
return "Evening";
// otherwise it's night
else return "Night";
}
Test it with some significant values:
public static void main(String[] args) throws IOException {
List<String> times = List.of(
// morining
"04:01", "10:59", "11:00",
// afternoon
"11:01", "14:59", "15:00",
// evening
"15:01", "18:29", "18:30",
// night
"18:31", "03:59", "04:00"
);
times.forEach(
time -> System.out.println(
String.format("%s is in the %s",
time, dayTime(time))));
}
Output:
04:01 is in the Morning
10:59 is in the Morning
11:00 is in the Morning
11:01 is in the Afternoon
14:59 is in the Afternoon
15:00 is in the Afternoon
15:01 is in the Evening
18:29 is in the Evening
18:30 is in the Evening
18:31 is in the Night
03:59 is in the Night
04:00 is in the Night