我想以两位数的格式显示日期,我希望它是这样的:当月或日是一个单一的数字时,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));
Calendar cale = Calendar.getInstance();
Date tasktime=cale.getTime();
SimpleDateFormat df=new SimpleDateFormat("yyyy-M-d");
System.out.println(df.format(tasktime));
来个简单点的方法,判断日期位数后后手动添加"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));
我就想问一下楼上的 month % 10 == 0 是什么? if( 4%10==0) 我想问 难道不是 false吗? 要么 if(month<10),要么month /10