在java中有没有其他简单的方法将" Aug/2016 " 转换为" 08/2016 "?
我的逻辑工作正常,但似乎太模糊了.
String myDate = "Aug/2016";
String str = myDate.split("/")[0];
Date date;
try {
date = new SimpleDateFormat("MMMM").parse(str);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
System.out.println("##### ----- month in number : " +cal.get(Calendar.MONTH+1));
int year = Calendar.getInstance().get(Calendar.YEAR);
int mnth = cal.get(Calendar.MONTH) + 1;
str = String.valueOf(mnth+"/"+year);
System.out.println("##### ----- new selected date : " +str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
建议请!...
只需使用SimpleDateFormat:
String myDate = "Aug/2016";
SimpleDateFormat parser = new SimpleDateFormat("MMM/yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("MM/yyyy");
System.out.println(formatter.format(parser.parse(myDate)));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74 次 |
| 最近记录: |