2012-07-28 90 views
2

我想我一個月轉換成使用SimpleDateFormat的MMM的格式,但我無法將其轉換。轉換成月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

下工作(我只是測試它)

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

您實際上並未使用您創建的格式。嘗試:

String month = format.format(tempmonth); 

您可以在一次格式化整個日期:

String dateString = sdf.format(cal.getTime()); 
0

我會說使用Joda Time,將使這樣的事情更容易。

無論如何,這裏this問題有幾個答案。