javaandroiddatesimpledateformatdate-format

Android SimpleDateFormat problem


SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date)formatter.parse("2011-09-13");
Log.e(MY_DEBUG_TAG, "Output is "+ date.getYear() + " /" + date.getMonth() + " / "+ (date.getDay()+1));

Is out putting

09-13 14:20:18.740: ERROR/GoldFishActivity(357): Output is 111 /8 / 3

What is the issue?


Solution

  • The methods you are using in the Date class are deprecated.

    You should use the Calendar class instead.

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Date date = (Date)formatter.parse("2011-09-13");        
    Calendar cal = Calendar.getInstance();  
    cal.setTime(date);
    Log.e(MY_DEBUG_TAG, "Output is "+ cal.get(Calendar.YEAR)+ " /" + (cal.get(Calendar.MONTH)+1) + " / "+ cal.get(Calendar.DAY_OF_MONTH));