Search code examples
javaandroiddatecalendar

Displaying date in a double digit format


I am having trouble displaying the date in a double digit format. I want it to be so that when the day or the month is a single digit example: 4 It would display 04. I'm having trouble coming up with the logic for it, if somebody can help me I would be really grateful.

Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int month = c.get(Calendar.MONTH);

        if (month % 10 == 0) {

            Place = 0 + month;
        }
        String Dates = year + "-" + Place + "-" + day;
        Date.setText((Dates));

Solution

  • DecimalFormat mFormat= new DecimalFormat("00");
    mFormat.format(Double.valueOf(year));
    

    in your case:

     mFormat.setRoundingMode(RoundingMode.DOWN);
     String Dates =  mFormat.format(Double.valueOf(year)) + "-" +  mFormat.format(Double.valueOf(Place)) + "-" +  mFormat.format(Double.valueOf(day));