4

有没有办法我可以通过“MM/dd”格式获取日期,但月份不是一个,而是像“Jan 30”这样int的缩写?string

目前我通过一个简单的 Java 做到了这一点Switch Case,但我想知道这是否已经包含在 aDateFormat或类似的东西中。

无论如何,我在这里留下我当前的代码。我使用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);
}
4

1 回答 1

12

试试这样:

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()));

您可以在此处查看可用格式选项的列表。

于 2012-07-03T13:39:01.320 回答