-3

有人可以帮我解决这个问题吗?我需要在android中以特定日期的单词来查找月份。我正在使用以下代码来获取星期几(我已经通过了当天的参数。)不理解如何获得月份。这将返回星期几:

public static String getFullDayName(int day_,int year_,int month_) 
    {
        Calendar c = Calendar.getInstance();
        // date doesn't matter - it has to be a Monday
        // I new that first August 2011 is one ;-)
        c.set(year_, month_-1, 1, 0, 0, 0);
        // c.set(2013,7,03);
        c.add(Calendar.DAY_OF_MONTH,day_-1);
        return String.format("%tA", c);
    }

我尝试了以下代码来获取一年中的月份,但没有得到答案(月份)。而是给了我星期几。

4

2 回答 2

1

如下更新您的方法

public static String getFullDayName(int day_,int year_,int month_) 
    {
        Calendar c = Calendar.getInstance();
        // date doesn't matter - it has to be a Monday
        // I new that first August 2011 is one ;-)
        c.set(year_, month_-1, 1, 0, 0, 0);
        // c.set(2013,7,03);
        c.add(Calendar.DAY_OF_MONTH,day_-1);

        // Option 1 
        // String month = c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US); // Returns like August
        // Option 2 
        // String month = c.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US); // Returns like Aug      

        // And other option can be      
        String month = (String) android.text.format.DateFormat.format("MMM", c.getTime()); // Returns like Aug

        return month;
    }
于 2013-08-05T10:09:13.457 回答
0

尝试

 int month = c.get(Calendar.MONTH);
于 2013-08-05T10:07:22.500 回答