2013-10-24 19 views
2

我想在下面的代码中将String (returns string :odb.getCloseDate())转换为Date。但我没有得到这个合作伙伴的输出"23/10/2013"。请纠正我在哪里做错了。 在数据库表中的值是在这个formate:2013-06-30和im通过bean检索这个数据,即odb.getCloseDate()。现在我需要在这个合成器中显示这个日期,即23/10/2013。使用java在xls中将字符串转换为日期格式

 HSSFCell row1 = row.createCell((short) j); 
     //row1.setCellType(row1.CELL_TYPE_NUMERIC); 

     try 
     { 
      //row1.setCellValue(Date.parse(odb.getCloseDate())); 
      DateFormat formatter; 
      Date date; 
      formatter = new SimpleDateFormat("dd/MM/yyyy"); 
      row1.setCellValue(date = formatter.parse(odb.getCloseDate())); 

     } 
     catch (Exception e) 
     { 
      row1.setCellValue(odb.getCloseDate()); 
     } 

回答

3

Date and Time Patterns

mm表示MinutesMM表示Month

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

,而不是

formatter = new SimpleDateFormat("dd/mm/yyyy"); 

试试这个,

 String dateValue = "2013-06-30"; //consider as your date. 
     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// First apply the patern to the incoming date value. Because it doesnt know the actual incming format 
     Date actualDate = simpleDateFormat.parse(dateValue); 
     simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
     System.out.println(simpleDateFormat.format(actualDate)); 
+0

我所做的更改,按您的建议。但没有得到这个合成的输出23/10/2013 .. Plz帮助 –

+0

删除cellType row1.setCellType(row1.CELL_TYPE_NUMERIC);将传入日期转换为字符串并将日期设置为字符串。 – newuser

+0

我在上面的代码中注释了行row1.setCellType(row1.CELL_TYPE_NUMERIC)并进行了检查。但没用。产量不会像2013年10月23日那样。 –

1

在你的日期格式mm应该MM

原因

m Minute in hour 

其中作为

M Month in year 

你需要Month in yearMinute in hour

然后你的格式化变得

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

Have a look on different formats here.

+0

我纠正了上面的代码。但我没有得到预期的结果。请纠正我在哪里做错了。(返回类型字符串在formate odb.getCloseDate()=“2013-06-30”) –

+0

@WorkWorld更新您的文章。字符串的当前值,预期值和当前输出 –

+0

嗨,Suresh,我编辑了上述评论。 plz帮助。 –

相关问题