android应用程序代码中的“%02d”

时间:2014-11-05 20:59:58

标签: java android eclipse format

应用程序代码

DateTime startDate8 = DateTime.now(); 
DateTime endDate8 = new DateTime(2014, 11, 5, 15, 0);

Period period8 = new Period(startDate8, endDate8, PeriodType.dayTime());

PeriodFormatter formatter8 = new PeriodFormatterBuilder()
    .appendMinutes()
    .toFormatter(); 

tw.setText(String.format("%02d",formatter8.print(period8))); 

申请不行。 应用已经停止了。我的代码有什么问题?

1 个答案:

答案 0 :(得分:1)

PeriodFormatter.print()返回String(而"%02d需要int)。我想你想要像

这样的东西
 // tw.setText(String.format("%02d",formatter8.print(period8))); 
tw.setText(formatter8.print(period8)); 

或者,将String解析为int

try {
    int val = Integer.parseInt(formatter8.print(period8));
    tw.setText(String.format("%02d",val));
} catch (NumberFormatException nfe) {
    nfe.printStackTrace();
}