java 使用“ShortenedMonth/dd”格式获取日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11312129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:37:57  来源:igfitidea点击:

Getting a Date with "ShortenedMonth/dd" format

javadatedate-format

提问by Sergio Carneiro

Is there a way I can get a date by the format "MM/dd" but instead of the month being an int, it is a shortened stringlike "Jan 30"?

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

Currently I did this with Java by a simple Switch Case, but I want to know if this is already contained in a DateFormator something similar.

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

Anyway I leave here my current code. I used R.stringbecause my application has multi-language support.

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

回答by Caner

Try like this:

像这样尝试:

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

You can see a list of available format options here.

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