转换日期从m/d/yy到mm/dd/yyyy[重复]

时间:2022-09-27 23:10:41

Possible Duplicate:
Conversion of Date
How to convert date from m/d/yy to mm/dd/yyyy?

可能的副本:日期的转换如何转换日期从m/d/yy到mm/dd/yyyy?

I get the date in a String as: 5/1/12. I need to convert it to 05/01/2012. Please suggest.

我把日期写成:5/1/12。我需要把它转换成05/01/2012。请建议。

I used the following code:

我使用了以下代码:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

d_date = dateFormat.parse("5/1/12"); 

strDate = dateFormat.format(d_date);

Result:
05/01/0012

Expected Result: 05/01/2012

预期结果:05/01/2012

Thanks Prasad

由于普拉萨德

1 个解决方案

#1


5  

Try the following:

试试以下:

DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("5/1/12");

formatter = new SimpleDateFormat("MM/dd/yyyy");
date = (Date)formatter.format(date);

The following links should prove helpful for more complicated tasks:

以下链接将有助于完成更复杂的任务:

Oracle SimpleDateFormat Docs

甲骨文SimpleDateFormat文档

Example code

示例代码

#1


5  

Try the following:

试试以下:

DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("5/1/12");

formatter = new SimpleDateFormat("MM/dd/yyyy");
date = (Date)formatter.format(date);

The following links should prove helpful for more complicated tasks:

以下链接将有助于完成更复杂的任务:

Oracle SimpleDateFormat Docs

甲骨文SimpleDateFormat文档

Example code

示例代码