转换日期:D-M-YYYY,以DD-MM-YYYY日期、YYYY、MM、DD

2023-09-04 07:30:49 作者:囚宠

可能重复:   日期转换

Possible Duplicate: Conversion of Date

我得到的日期从对话框为:2012年5月1日 - 作为一个字符串

I get the date from dialog as: 5-1-2012 - as a String.

我需要将其转换为05-01-2012,字符串为好。什么是做到这一点的simpliest方式?

I need to convert it to 05-01-2012, string as well. What is the simpliest way to do it?

推荐答案

你的意思是?

String date = "5-1-2012";
if (date.charAt(1) == '-') date = "0" + date;
if (date.charAt(4) == '-') date = date.substring(0,3) + "0" + date.substring(3);
// date is 05-01-2012