Java转换字符串(日/月/年)格式为Date [英] Java Converting String(day/month/year) format to Date
本文介绍了Java转换字符串(日/月/年)格式为Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将字符串转换为日期
I am trying to conver the string to a date
String date = "12/31/2012";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
try {
Date parse = sdf.parse(date);
System.out.println(parse);
} catch (ParseException ex) {
ex.printStackTrace();
}
但在我看来,它有一个例外
。的 ParseExpception
。这是为什么?我想生成 12/31/2012的日期
But it appears to me that it is having an Exception
. of ParseExpception
. why is that? I want to generate a date for 12/31/2012
推荐答案
日期字符串 - 12/31/2012
它与 - MM / dd / yyyy
- d - 一天中的日子
- M - 月年份
- y - 年份
...
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
这篇关于Java转换字符串(日/月/年)格式为Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文