之前一直用如下方法进行字符串的格式化:
DecimalFormat df = new DecimalFormat("0000");
String str= df.format(12);
结果为:"0012"
今天突然发现一篇文章用如下方法实现:
String str = String.format("%04d", 12); 结果:"0012"
String str = String.format("%4d", 12); 结果:" 12"
String str = String.format("%,d", 121212); 结果:"121,212"
String str = String.format("%1$5s", "abc");结果:" abc"
String str = String.format("%1$tF %1$tT", new Date());结果:"2009-01-19 17:39:25"
String.format("%1$tY%1$tm%1$td%1$tH%1$tM%1$tS%1$tL", new Date())结果:20090414092113796
String.format("%1$tm", new Date());结果为月份
String.format("%1$tY%1$tm%1$td%1$tH%1$tM%1$tS", new Date());