2

我正在尝试使用 SimpleDateFormat 将我的月份转换为 MMM 格式,但我无法转换它。

    tvDisplayDate = (TextView) findViewById(R.id.dateDisplay);

    Calendar cal=Calendar.getInstance();

    SimpleDateFormat format = new SimpleDateFormat("MMM");

    int tempyear = cal.get(Calendar.YEAR);
    int tempmonth = cal.get(Calendar.MONTH);
    int tempday = cal.get(Calendar.DAY_OF_MONTH);

    String month = new Integer(tempmonth).toString();
4

3 回答 3

4

以下作品(我刚刚测试过)

Calendar cal=Calendar.getInstance();
int tempmonth = cal.get(Calendar.MONTH);

SimpleDateFormat newformat = new SimpleDateFormat("MMM");
SimpleDateFormat oldformat = new SimpleDateFormat("MM");

        String monthName = null;
        Date myDate;
        try {
            myDate = oldformat.parse(String.valueOf(tempmonth));
            monthName = newformat.format(myDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Log.d("MMM", monthName);
于 2012-07-28T15:39:48.797 回答
0

您实际上并没有使用您创建的格式。尝试:

String month = format.format(tempmonth);

您可以使用以下命令一次格式化整个日期:

String dateString = sdf.format(cal.getTime());
于 2012-07-28T15:39:32.260 回答
0

我会说使用Joda Time会让这些事情变得更容易。

无论如何,这里的这个问题有几个答案。

于 2012-07-28T15:44:23.413 回答