2 hcg 21 hcg_21 于 2013.04.18 13:58 提问

用两位数的格式显示日期

我想以两位数的格式显示日期,我希望它是这样的:当月或日是一个单一的数字时,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));

3个回答

xiaoyan_12
xiaoyan_12   2013.04.19 10:47
已采纳
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));
ppgl1988
ppgl1988   2013.04.18 15:30
    Calendar cale = Calendar.getInstance();

    Date tasktime=cale.getTime();

    SimpleDateFormat df=new SimpleDateFormat("yyyy-M-d");

    System.out.println(df.format(tasktime));
hiramtan
hiramtan   2013.04.18 17:05

来个简单点的方法,判断日期位数后后手动添加"0",加粗部分为修改内容.

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

string dayString;
if(day<10)
dayString = "0"+day;
else
dayString = day.toSring();

    int month = c.get(Calendar.MONTH);

    if (month % 10 == 0) {

        Place = 0 + month;
    }

String Dates = year + "-" + Place + "-" + dayString;
//String Dates = year + "-" + Place + "-" + day;
Date.setText((Dates));

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!