有没有办法可以按“MM / dd”格式获取日期,而不是月份是int
,它是缩短的string
,如“1月30日”?
目前我通过一个简单的Switch Case
用Java做到了这一点,但我想知道这是否已包含在DateFormat
或类似内容中。
无论如何,我离开这里是我现在的代码。我使用R.string
因为我的应用程序支持多语言。
public void setShortMonth(){
final Calendar calendar = Calendar.getInstance();
int Month = calendar.get(Calendar.MONTH);
String ShortMonth = " ";
switch (Month){
case 0:
ShortMonth = getString(R.string.Jan);
break;
case 1:
ShortMonth = getString(R.string.Fev);
break;
case 2:
ShortMonth = getString(R.string.Mar);
break;
case 3:
ShortMonth = getString(R.string.Apr);
break;
case 4:
ShortMonth = getString(R.string.May);
break;
case 5:
ShortMonth = getString(R.string.Jun);
break;
case 6:
ShortMonth = getString(R.string.Jul);
break;
case 7:
ShortMonth = getString(R.string.Aug);
break;
case 8:
ShortMonth = getString(R.string.Sep);
break;
case 9:
ShortMonth = getString(R.string.Oct);
break;
case 10:
ShortMonth = getString(R.string.Nov);
break;
case 11:
ShortMonth = getString(R.string.Dec);
break;
}
TextView datetxt = (TextView) findViewById(R.id.home_date);
datetxt.setText(ShortMonth + " " + Day);
}
答案 0 :(得分:10)
试试这样:
final Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd"); // 3-letter month name & 2-char day of month
TextView datetxt = (TextView) findViewById(R.id.home_date);
datetxt.setText(formatter.format(calendar.getTime()));
您可以看到可用格式选项列表here。