可以使用 Java 的时间日期类库来处理时间字符串,如下所示:

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String timeString = '2023-09-20 00:01';
        // 解析时间字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm');
        LocalTime time = LocalTime.parse(timeString, formatter);
        // 格式化为时:分的字符串
        String formattedTime = time.format(DateTimeFormatter.ofPattern('HH:mm'));
        System.out.println(formattedTime);
    }
}

运行以上代码,输出结果为:

00:01

这样就可以将时间字符串'2023-09-20 00:01'切割并格式化为'00:01'。

Java 时间字符串切割:保留时分并格式化成 HH:mm

原文地址: http://www.cveoy.top/t/topic/fh5n 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录