java时间格式 YYYY-M-D 转换为 YYYY-MM-DD
// 输入时间为 2024-8-9 转换为 2024-08-09
public static void main(String[] args) {
String inputDate = "2021-2-2";
String[] Add = inputDate.split("-");
StringBuilder addBuilder = new StringBuilder();
for (int i = 0; i < Add.length; i++) {
if(i ==0){
addBuilder.append(Add[i]);
}else {
if(Add[i].length()>1){
addBuilder.append("-"+ Add[i]);
}else {
addBuilder.append( "-0" + Add[i]);
}
}
}
System.out.println("时间是: " + addBuilder);
}
输出结果是
时间是: 2021-02-02