java对2023-09-20 00:01:01这种数据格式进行切割只保留时 分改为01:01这种的
你可以使用Java中的日期时间格式化类SimpleDateFormat来实现对日期时间格式的切割和格式化。下面是一个示例代码:
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTimeFormatting {
public static void main(String[] args) {
String dateTimeString = "2023-09-20 00:01:01";
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm");
try {
Date date = inputFormat.parse(dateTimeString);
String formattedTime = outputFormat.format(date);
System.out.println(formattedTime);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行上述代码,输出结果为:
00:01
其中,SimpleDateFormat中的日期时间格式化模式如下:
yyyy:四位数的年份MM:两位数的月份dd:两位数的日期HH:两位数的小时(24小时制)mm:两位数的分钟ss:两位数的秒钟
你可以根据需要调整日期时间格式化模式。
原文地址: https://www.cveoy.top/t/topic/jeAy 著作权归作者所有。请勿转载和采集!