我无法以两位数格式显示日期。我希望这样,以便当日期或月份为个位数示例时:4它会显示04。如果要有人帮助我,我很难提出它的逻辑,我将不胜感激。

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

最佳答案

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

在您的情况下:
 mFormat.setRoundingMode(RoundingMode.DOWN);
 String Dates =  mFormat.format(Double.valueOf(year)) + "-" +  mFormat.format(Double.valueOf(Place)) + "-" +  mFormat.format(Double.valueOf(day));

关于java - 以两位数格式显示日期,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10203924/

10-09 19:38