将日期设置为“dd” – “mm” – “yy”格式来自“dd” – “mm” – “yyyy”在java中

我的日期格式为dd-mm-yyyy格式(在我的数据库中)。我需要将其转换为dd-mm-yy格式。我从db检索日期并将其存储在字符串中。

String date=doc.getString("date"); for example: 05-19-1990 to 05-19-90. 

1990年并不是完全需要只需要90.在java中使用split()我尝试了一些东西,但它不会帮助我。任何人都可以帮助。任何帮助将受到高度赞赏…….

试试下面的代码

  try { SimpleDateFormat sdf= new SimpleDateFormat("MM-dd-yy"); Date d = sdf.parse("05-19-1990"); System.out.println(sdf.format(d)); } catch (ParseException ex) { ex.printStackTrace(); } 

使用DateFormat来解决该问题:

 DateFormat df = SimpleDateFormat("dd-MM-yyyy"); DateFormat df1 = SimpleDateFormat("dd-MM-yy"); df1.format(df.parse(date)); 

使用两个SimpleDateFormat ,一个解析它,一个格式化它。

 SimpleDateFormat in = new SimpleDateFormat("MM-dd-yyyy"); Date inDate = in.parse(date); // 05-19-1990 SimpleDateFormat out = new SimpleDateFormat("MM-dd-yy"); String newDate = out.format(inDate); 

要么

 SimpleDateFormat out = new SimpleDateFormat("dd-MM-yy"); String newDate = out.format(inDate); 

如果您真的想要19-05-90 ,根据您的问题标题;)

有关更多详细信息,请查看java.text.SimpleDateFormat

试试这个..

 DateFormat df2 = new SimpleDateFormat("dd-mm-yy"); String formattedDate2 = df2.format(theDate);