Search code examples
javaandroidcalendar

Calendar return wrong current date android


Why does this code return 0001-02-05?

public static String getNowDate() throws ParseException
{        
    return Myformat(toFormattedDateString(Calendar.getInstance()));
}

I changed the code to:

public static String getNowDate() throws ParseException
{        
    Calendar temp=Calendar.getInstance();
    return temp.YEAR+"-"+temp.MONTH+"-"+temp.DAY_OF_MONTH;
}

And now it returns 1-2-5.

Please, help me get the actual date. all i need is the Sdk date.


Solution

  • Use SimpleDateFormat

    new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
    

    You are using constants to be used with the Calendar.get() method.