我已经成功地将日期字符串的格式从yyyy-MM-dd
更改为MM/dd
。现在的问题是,如果month
和day
的值都小于10,则格式应该是例如2/7
而不是02/07
。我该怎么做呢?如何在整数中省略0日期字符串
这里是我的代码:
public static String cahngeFormat(String dateTime) {
Date date = null;
String str = dateTime;
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
date = formatter.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 2);
formatter = new SimpleDateFormat("MM/dd");
str = formatter.format(calendar.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
你解决了你的问题吗? – dipali