String str_date = currentMonth + 1 + "-" + value.toString() + "-" + currentYear;
DateFormat formatter ;
Date date = null ;
formatter = new SimpleDateFormat("dd-MMM-yy");
try {
date = (Date)formatter.parse(str_date);
}
catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date);
嗨,我试图搜索字符串到目前为止几乎每个人都使用这种代码,但似乎仍然打印出null?谢谢。
答案 0 :(得分:0)
尝试
formatter = new SimpleDateFormat("dd-MM-yyyy");
或
formatter = new SimpleDateFormat("MM-dd-yyyy");
取决于你真正想要的东西。您的代码建议您可能希望将月份作为模式的第一部分。
确保currentMonth + 1
符合您的要求。如果currentMonth
为12,那么+1
会将日期推迟到明年的1月。
另外,尝试习惯使用Locale
。默认的一个可能适合你,但会打破别人的电脑。