应用程序代码是
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)));
应用程序无法运行。 不幸的是,应用程序已停止。我的代码有什么问题?
最佳答案
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();
}
关于java - Android应用程序代码中的"%02d",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26767004/