2012-12-03 47 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。默認的一個可能適合你,但會破壞別人的電腦。