java 将日期从 m/d/yy 转换为 mm/dd/yyyy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10405052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:54:52  来源:igfitidea点击:

Convert date from m/d/yy to mm/dd/yyyy

java

提问by Prasad Kothamasu

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

谢谢普拉萨德

回答by ashurexm

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

Oracle SimpleDateFormat 文档

Example code

示例代码