Val*_*33 1 java string time datetime timeofday
我的字符串值;
09:00-10:00,12:00-14:30,16:00-18:00(这个字符串像这样重复时间间隔n次)
我想使用模式匹配找出字符串的格式是否正确;
Pattern.matches("<Pattern Here>", stringValue);
Run Code Online (Sandbox Code Playgroud)
是否可以?
我试过;
Pattern.matches("^[0-9:0-9-0-9:0-9,]+$", value);
Run Code Online (Sandbox Code Playgroud)
但无法正常工作
您可以检查字符串是否与正则表达式匹配^(?:(?:\d{2}:\d{2}\-\d{2}:\d{2})(?:,(?!$))?)*$。请注意,使用 时不需要放置开头(即^)和结尾(即) 。$String#matches
如果字符串通过了此验证,则将字符串拆分为,,-然后使用 java.time API 来验证各个时间字符串。
演示:
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(hasCorrectFormat("09:00-10:00,12:00-14:30,16:00-18:00")); // true
System.out.println(hasCorrectFormat("09:00-10:00,12:00-1:30,16:00-18:00")); // false -> 1:30 is not in desired
// format
System.out.println(hasCorrectFormat("09:00-10:00")); // true
System.out.println(hasCorrectFormat("09:00 10:00")); // false
System.out.println(hasCorrectFormat("09:00-10:00,09:00 10:00")); // false
System.out.println(hasCorrectFormat("09:00-10:00-12:00-14:30,16:00-18:00")); // false
System.out.println(hasCorrectFormat("09:00-10:00,12:00-14:30,16:00-18:00,")); // false
}
static boolean hasCorrectFormat(String strTimeRanges) {
if (!strTimeRanges.matches("(?:(?:\\d{2}:\\d{2}\\-\\d{2}:\\d{2})(?:,(?!$))?)*"))
return false;
String[] times = strTimeRanges.split("[-,]");
for (String time : times) {
try {
LocalTime.parse(time);
} catch (DateTimeParseException e) {
return false;
}
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
true
false
true
false
false
false
false
Run Code Online (Sandbox Code Playgroud)
从Trail: Date Time中了解有关现代日期时间 API 的更多信息。
Ole VV 的宝贵评论促使我扩展了原来的答案,以展示如何使用 java.time API 来比较各个时间范围内的时间。我希望这能为学习者提供足够的提示,以便根据需求的复杂性进一步扩展解决方案。
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(hasCorrectFormatAndRanges("09:00-10:00,12:00-14:30,16:00-18:00")); // true
System.out.println(hasCorrectFormatAndRanges("09:00-10:00,12:00-1:30,16:00-18:00")); // false -> 1:30
System.out.println(hasCorrectFormatAndRanges("10:00-09:00,12:00-14:30,16:00-18:00")); // false -> 10:00-09:00
System.out.println(hasCorrectFormatAndRanges("09:00-10:00")); // true
System.out.println(hasCorrectFormatAndRanges("09:00 10:00")); // false
System.out.println(hasCorrectFormatAndRanges("09:00-10:00,09:00 10:00")); // false
System.out.println(hasCorrectFormatAndRanges("09:00-10:00-12:00-14:30,16:00-18:00")); // false
System.out.println(hasCorrectFormatAndRanges("09:00-10:00,12:00-14:30,16:00-18:00,")); // false
}
static boolean hasCorrectFormatAndRanges(String strTimeRanges) {
if (!strTimeRanges.matches("(?:(?:\\d{2}:\\d{2}\\-\\d{2}:\\d{2})(?:,(?!$))?)*"))
return false;
String[] timeRanges = strTimeRanges.split(",");
for (String timeRange : timeRanges) {
String[] times = timeRange.split("-");
try {
if (LocalTime.parse(times[1]).isBefore(LocalTime.parse(times[0])))
return false;
} catch (DateTimeParseException e) {
return false;
}
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
true
false
false
true
false
false
false
false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93 次 |
| 最近记录: |