所以我有这个代码...
SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:minutes");
poaTimeDataText.setText(timeFormatter.format(getIntent().getLongExtra(
ShiftLogsFragment.EXTRA_POA_TIME, 0)));
现在,而不是显示例如 02:40,我希望它显示 2 小时 40 分钟。但是,我已经尝试过,将模式更改为"HH hours mm minutes"
但编译器不喜欢那样。我怎样才能得到想要的结果?
编辑:如果它考虑单数或复数,如小时或小时、分钟(如果是 0 或 1)或分钟(如果是 2 或更多),那就太好了
创建一个 String 变量并像这样将您的字符串分配给它
String myString = timeFormatter.format(getIntent().getLongExtra(ShiftLogsFragment.EXTRA_POA_TIME, 0))
然后像这样拆分你的字符串
String[] separated = myString.split(":");
现在你有每个变量
separated[0]; // this will contain "hours"
separated[1]; // this will contain "minutes"
定义两个变量
String hours = "";
String minutes = "";
做一些测试
if (separated[0].equals("0") || separated[0].equals("1")) {
hours = "hour";
} else {
hours = "hours";
}
if (separated[1].equals("0") || separated[1].equals("1")) {
minutes = "minute";
} else {
minutes = "minutes";
}
最后像这样将此字符串设置为您的 textView
poaTimeDataText.setText(separated[0] + hours + separated[1] + minutes);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句