从用户提供的时间中减去 45 分钟
Subtract 45 minutes from time that user provided
我正在尝试输入格式为 HH:mm(24 小时格式)的字符串,然后从给定时间减去 45 分钟以为用户提供新时间。这是我到目前为止完成的代码,但我一直收到不完整的类型错误。我是否错误地使用了 minusMinutes()?
编辑添加错误信息:
“发现不兼容的类型:'java.time.LocalDateTime',需要:'java.lang.String':52'
public static boolean validateTime(String timeStr)
{
//Regular expression logic to validate correct format:
// ( = start of group, [01]?[0-9] time can start with 0-9,1-9, 00-09,10-19
// | = or, 2[0-5] represents start time with 20-23, ) = end group
//
String regex = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
Pattern p = Pattern.compile(regex);
//if time empty return false
if (timeStr == null)
{
return false;
}
//matcher method attempts to find match between given time and regex
Matcher m = p.matcher(timeStr);
//if given time and Regular expression matches,return true.
return m.matches();
}
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Enter time in 24 hr format (HH:mm): ");
String timeIn = myScanner.nextLine(); // read user input
while(!(validateTime(timeIn)))
{
System.out.println("Please enter valid time: ");
timeIn = myScanner.nextLine();
}
System.out.println("Current Time is: " + timeIn);
//String[] arrOfStr = timeIn.split("\:");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalDateTime datetime = LocalDateTime.parse(timeIn,formatter);
String updatedTime = datetime.minusMinutes(45);
System.out.println("New time to wake up: " + updatedTime);
您需要添加一个toString()
来将时间对象字符串化。此外,由于您只阅读一次,因此请使用 LocalTime
而不是 LocalDateTime
您的最后几行将如下所示:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime datetime = LocalTime.parse(timeIn, formatter);
String updatedTime = datetime.minusMinutes(45).toString();
System.out.println("New time to wake up: " + updatedTime);
我正在尝试输入格式为 HH:mm(24 小时格式)的字符串,然后从给定时间减去 45 分钟以为用户提供新时间。这是我到目前为止完成的代码,但我一直收到不完整的类型错误。我是否错误地使用了 minusMinutes()?
编辑添加错误信息: “发现不兼容的类型:'java.time.LocalDateTime',需要:'java.lang.String':52'
public static boolean validateTime(String timeStr)
{
//Regular expression logic to validate correct format:
// ( = start of group, [01]?[0-9] time can start with 0-9,1-9, 00-09,10-19
// | = or, 2[0-5] represents start time with 20-23, ) = end group
//
String regex = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
Pattern p = Pattern.compile(regex);
//if time empty return false
if (timeStr == null)
{
return false;
}
//matcher method attempts to find match between given time and regex
Matcher m = p.matcher(timeStr);
//if given time and Regular expression matches,return true.
return m.matches();
}
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Enter time in 24 hr format (HH:mm): ");
String timeIn = myScanner.nextLine(); // read user input
while(!(validateTime(timeIn)))
{
System.out.println("Please enter valid time: ");
timeIn = myScanner.nextLine();
}
System.out.println("Current Time is: " + timeIn);
//String[] arrOfStr = timeIn.split("\:");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalDateTime datetime = LocalDateTime.parse(timeIn,formatter);
String updatedTime = datetime.minusMinutes(45);
System.out.println("New time to wake up: " + updatedTime);
您需要添加一个toString()
来将时间对象字符串化。此外,由于您只阅读一次,因此请使用 LocalTime
而不是 LocalDateTime
您的最后几行将如下所示:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime datetime = LocalTime.parse(timeIn, formatter);
String updatedTime = datetime.minusMinutes(45).toString();
System.out.println("New time to wake up: " + updatedTime);