将yyyy-MM-dd HH:mm:ss这样的字符串切割成HH:mm:ss的字符串
时间: 2024-10-23 12:11:23 浏览: 26
将 "yyyy-MM-dd HH:mm:ss" 这样的日期时间字符串分割成单独的小时、分钟和秒部分,你可以使用编程语言中的字符串切分或正则表达式功能。下面是一个简单的例子,假设我们使用 Java 语言:
```java
String dateTimeStr = "2023-03-15 18:30:45";
Pattern pattern = Pattern.compile("\\s*(\\d{4})-(\\d{2})-(\\d{2}) \\s*(\\d{2}):(\\d{2}):(\\d{2})");
Matcher matcher = pattern.matcher(dateTimeStr);
if (matcher.matches()) {
String year = matcher.group(1);
String month = matcher.group(2);
String day = matcher.group(3);
String hour = matcher.group(4);
String minute = matcher.group(5);
String second = matcher.group(6);
// 输出:18:30:45
System.out.println(hour + ":" + minute + ":" + second);
}
```
在这个例子中,`\\s*` 匹配任意空白字符(包括空格),`\d{4}`、`\d{2}` 和 `\d{1}` 分别匹配四位数字的年份、两位数字的月份和天数,以及小时、分钟和秒。
阅读全文
相关推荐


















