2023-08-25T080006 怎么转化成yyyy-MM-dd HHmmss 用java实现
要将一个日期时间字符串转换为指定格式的字符串,可以使用Java的SimpleDateFormat类来实现。
以下是将"2023-08-25T08:00:06"转换为"yyyy-MM-dd HH:mm:ss"格式的代码示例:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
String dateTimeString = "2023-08-25T08:00:06";
String targetPattern = "yyyy-MM-dd HH:mm:ss";
// 创建日期时间格式化对象
DateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
DateFormat targetFormat = new SimpleDateFormat(targetPattern);
try {
// 将字符串解析为日期对象
Date dateTime = sourceFormat.parse(dateTimeString);
// 将日期对象格式化为指定格式的字符串
String formattedDateTime = targetFormat.format(dateTime);
System.out.println(formattedDateTime);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行以上代码,输出结果为:"2023-08-25 08:00:06"

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