2012-12-03 44 views
-3
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); 

嗨,我试图搜索字符串到目前为止,几乎每个人都使用这种类型的代码,但它似乎仍然打印出空?谢谢。当前解析字符串

+5

在解析之前,你看过'str_date'中的实际内容吗? –

+0

请不要这样做。这在很多方面都是错误的。 – duffymo

+1

...一旦你这样做了,也许可以使用格式化程序来格式化一个实际的日期('new Date()'),以确保你看到的与你期望的一致 –

回答

0

尝试要么

formatter = new SimpleDateFormat("dd-MM-yyyy"); 

formatter = new SimpleDateFormat("MM-dd-yyyy"); 

取决于你真正想要的东西。你的代码表明你可能想要有一个月作为你模式的第一部分。

确保currentMonth + 1是你想要的。如果currentMonth是12,则+1将推迟到明年1月的日期。

此外,也尝试习惯使用Locale。默认的一个可能适合你,但会破坏别人的电脑。