从字符串读取时间到"Morning"、"Afternoon"、"Evening"、"night"



所以我遇到了问题,输入是时间04:30的字符串,如果04.01直到11输出"早上";,11.01-15,输出为"0";下午";,15.01-18.30";晚上";,18.31-4是"0";夜晚";。哪一部分我做错了。

这是我的代码:

import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.next();
DateFormat dateFormat = new SimpleDateFormat("hh:mm");
Date d = dateFormat.parse(input);

if (d > 4 || d <= 11) {
System.out.println("Morning");
} else if (d > 11 || d <= 15) {
System.out.println("Afternoon");
} else if (d > 15 || d <= 18.3) {
System.out.println("Evening");
} else if (d > 18.3 || d <= 4) {
System.out.println("Night");
}            
}
}

这里有一个解决方案:

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

但是您不需要将输入字符串转换为日期/时间。

我做错了哪一部分

  1. 您正试图使用基本体的运算符来比较对象
  2. 解析一天中的时间的模式应该是HH:mm而不是hh:mm

备选方案:java.time

由于这都是关于一天中的时间,而不考虑日、月、年(甚至秒或更小的单位(,因此可以使用专门为此设计的类java.time.LocalTime

这里有一个可读的例子:

/**
* 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";
}

用一些重要的值来测试它:

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

输出:

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

为简洁起见,请使用LocalTime#isBefore

其他答案似乎是正确的,但可能更短。

首先,您的输入字符串符合ISO8601字符串格式,假设这些输入使用24小时时钟(00-23(。如果是这样,那么您的格式设置模式不正确,其中hh应为大写HH。而且,更重要的是,如果使用LocalTime进行解析,则不需要指定任何格式化模式。在解析/生成文本时,java.time类默认使用标准ISO 8601格式。

LocalTime lt = LocalTime.parse( "23:51" ) ;

我们可以通过进行五个测试而不是四个测试来删除组合谓词,从而消除&&。测试将时间与调用LocalTime#isBefore进行比较。注意第一个最后一个测试是如何返回"Night"的。

if( lt.isBefore( LocalTime.of( 4 , 0 ) ) ) { return "Night" ; }
else if( lt.isBefore( LocalTime.of( 11 , 0 ) ) ) { return "Morning" ; }
else if( lt.isBefore( LocalTime.of( 15 , 00 ) ) ) { return "Afternoon" ; }
else if( lt.isBefore( LocalTime.of( 18 , 30 ) ) ) { return "Evening" ; }
else { return "Night" ; }

因为比较是在小时和分钟上完成的。在小时和分钟上使用If…Else值可能会变得很重。

试试这个:

public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
String input = in.next();

LocalTime time = LocalTime.parse(input+":00.0");

if ( time.isAfter(LocalTime.parse("04:00:00.0")) && time.isBefore(LocalTime.parse("11:01:00.0"))){
System.out.println("Morning");
}
else if ( time.isAfter(LocalTime.parse("11:00:00.0")) && time.isBefore(LocalTime.parse("15:00:00.0"))){
System.out.println("Afternoon");
}else if ( time.isAfter(LocalTime.parse("15:00:00.0")) && time.isBefore(LocalTime.parse("18:30:00.0"))){
System.out.println("Evening");
}else{
System.out.println("Night");
}

}
}

试试这个:

可能性:1

import java.util.*;
public class JimRaynor {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.next();
String[] x = input.split(":");
int hour = Integer.parseInt(x[0]);
int minute = Integer.parseInt(x[1]);
if (hour > 4 && hour <= 11)
System.out.println("Morning");
else if (hour > 11 && hour <= 15)
System.out.println("Afternoon");
else if (hour > 15 && hour <= 18 && minute <= 3)
System.out.println("Evening");
else
System.out.println("Night");
}
}

O/P:-

12:03
Afternoon

可能性:2

import java.time.LocalTime;
import java.util.*;
public class JimRaynor {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.next();
String[] x = input.split(":");
LocalTime time = LocalTime.of(Integer.parseInt(x[0]), Integer.parseInt(x[1]));
int hour = time.getHour();
int minute = time.getMinute();
if (hour > 4 && hour <= 11)
System.out.println("Morning");
else if (hour > 11 && hour <= 15)
System.out.println("Afternoon");
else if (hour > 15 && hour <= 18 && minute <= 3)
System.out.println("Evening");
else
System.out.println("Night");
}
}

O/P:-

12:03
Afternoon

最新更新