2016-09-01 59 views
-3

我试图将MMM的日期,dd yyyy转换为dd/mm/yyyy格式以更新日期选择器,但是我得到了以下的解析方法错误:无法将MMM,dd yyyy的日期转换为dd/MM/yyyy在Android中

SimpleDateFormat format = new SimpleDateFormat("MMM, dd yyyy"); 
Date newDate = format.**parse**(strCurrentDate); 

format = new SimpleDateFormat("dd/MM/yyyy"); 
String date = format.format(newDate); 

String str[] = date.split("/"); 
int myday = Integer.parseInt(str[0]); 
int mymonth = Integer.parseInt(str[1]); 
int myyear = Integer.parseInt(str[2]); 
dp.updateDate(myday,mymonth,myyear); 

我在做什么错在这里?我应该用try/catch来附上它吗? 谢谢

+0

向我们展示示例日期字符串 –

+2

解析不被弃用? – Stefan

+0

那么为什么我得到java.text.ParseException?和我如何解决它? –

回答

0

这将确定工作。

String inputPattern = "MMM, dd yyyy"; 
     String outputPattern = "dd/MM/yyyy"; 
     SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern, Locale.ENGLISH); 
     SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern,Locale.ENGLISH); 

     Date date = null; 
     String str = null; 

     try { 
      String strCurrentDate = "Sep, 1 2016"; 
      date = inputFormat.parse(strCurrentDate);// it's format should be same as inputPattern 
      str = outputFormat.format(date); 
      Log.e("Log ","str "+str); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } catch (java.text.ParseException e) { 
      e.printStackTrace(); 
     } 
+0

我们修正了这个例外,但即使strCurrentDate的值为Sep,2016我也因此得到了空格 –

+0

对不起,我检查过“Sep,1 2016”,它工作的很完美。 查看我更新的代码片段 – ViramP

+0

在错误的地方逗号做了混乱,我认为这是嗯,DD YYYY,但它是嗯,DD,YYYY非常感谢你!!!为您的指导 –

0

您需要首先分析使用SimpleDateFormatter(SDF)的字符串喜欢如下

String strDate = "MM, dd yyyy"; 
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); 
Date dateStr = formatter.parse(strDate); 

使用此字符串解析成一个日期,然后你的其他SDF把这一日期到你想要的格式。

+0

请在回答问题之前阅读问题!谢谢 –

+0

如果你没有足够的技巧来理解答案,我做不到。 –

+0

我的意思是我们说同样的事情!你回答我的问题实际上 –

相关问题