java判断今天中午吃什么
时间: 2023-08-23 16:07:00 浏览: 40
您可以使用Java的日期时间类(如`java.time.LocalDate`和`java.time.LocalTime`)来判断当前日期和时间,然后根据您设定的规则决定中午吃什么。以下是一个简单的示例代码:
```java
import java.time.LocalTime;
public class LunchDecider {
public static void main(String[] args) {
LocalTime noon = LocalTime.of(12, 0); // 中午12点
LocalTime currentTime = LocalTime.now(); // 当前时间
if (currentTime.isBefore(noon)) {
System.out.println("中午还没到,等一会再决定吃什么吧!");
} else {
System.out.println("现在是中午,可以吃午餐了!");
// 根据你的规则决定中午吃什么
}
}
}
```
当然,这只是一个简单的示例代码,您可以根据自己的需求进行更改和扩展。
相关问题
Java写一个 中午模糊匹配 demo
以下是一个Java写的中午模糊匹配demo,它可以匹配输入的字符串是否包含关键词,并返回匹配结果:
```java
import java.util.Scanner;
public class ChineseMatchDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String[] keywords = {"中午", "午餐", "吃饭", "餐厅"}; // 关键词列表
boolean match = false;
for (String keyword : keywords) {
if (input.contains(keyword)) {
match = true;
break;
}
}
if (match) {
System.out.println("匹配成功!");
} else {
System.out.println("未匹配到相关内容。");
}
}
}
```
在上面的代码中,我们首先定义了一个关键词列表,包含了一些与中午用餐相关的关键词。然后通过循环遍历关键词列表,判断输入的字符串中是否包含了任意一个关键词,如果包含则表示匹配成功,否则未匹配到相关内容。
java17实现24小时制,分为两个连续区间,传入时间段,判断是哪个区间
可以使用Java 17中的LocalTime类来实现24小时制的时间处理。下面是一个示例代码,可以将一天分为两个连续的区间(00:00-11:59和12:00-23:59),并判断传入的时间段属于哪个区间:
```java
import java.time.LocalTime;
public class TimeRange {
public static void main(String[] args) {
LocalTime startTime = LocalTime.parse(args[0]);
LocalTime endTime = LocalTime.parse(args[1]);
LocalTime noon = LocalTime.of(12, 0);
if (startTime.isBefore(noon) && endTime.isBefore(noon)) {
System.out.println("The time range is in the first half of the day (00:00-11:59).");
} else if (startTime.isAfter(noon) && endTime.isAfter(noon)) {
System.out.println("The time range is in the second half of the day (12:00-23:59).");
} else {
System.out.println("The time range straddles both halves of the day.");
}
}
}
```
在这个示例中,我们首先使用LocalTime的parse()方法将传入的时间字符串解析为LocalTime对象。然后我们定义了一个中午的时间(12:00),并使用isBefore()和isAfter()方法来判断时间段的起始时间和结束时间是在中午之前还是之后。最后根据判断结果输出相应的区间信息。
注意,为了让这段代码运行起来,你需要传入两个时间参数(格式为hh:mm:ss),例如:
```
java TimeRange 09:30:00 14:45:00
```
这将打印出:
```
The time range straddles both halves of the day.
```