我试图仅从Date对象获取日期,但目前我只得到以下输出:
"Thu Nov 24 17:46:14 GMT +02:00 2016"
我希望得到:
"24/11/2016"
这是我的代码:
public String getNextYearDate(){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1); // to get previous year add -1
Date nextYear = cal.getTime();
return nextYear.toString();
答案 0 :(得分:3)
只需将SimpleDateFormat
与dd/MM/yyyy
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
String output = df.format(nextYear);
答案 1 :(得分:0)
您可以尝试以下代码:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1);
int mYear = cal.get(Calendar.YEAR);
int mMonth = (cal.get(Calendar.MONTH))+1;
int mDay = cal.get(Calendar.DAY_OF_MONTH);
String result = ""+mDay+"/"+mMonth+"/"+mYear ;