【发布时间】:2020-12-16 07:42:11
【问题描述】:
我已经编写了 if-else 条件来显示日期和时间,例如 1 天、1 小时等。我想知道是否有任何函数可以在不添加这些条件的情况下执行此操作。我在这里发布我的代码
if(days.toInt() == 0 && hours.toInt() > 1)
{
result = "$hours hours"
}
else if(hours.toInt() == 0 && days.toInt() > 1)
{
result = "$days days"
}
else if(days.toInt() == 1 && hours.toInt() > 1)
{
result = "$days day $hours hours"
}
else if(hours.toInt() == 1 && days.toInt() > 1)
{
result = "$days days $hours hour"
}
else if(days.toInt() == 1 && hours.toInt() == 0)
{
result = "$days day"
}
else if(days.toInt() == 0 && hours.toInt() == 1)
{
result = "$hours hour"
}
else if(days.toInt() == 1 && hours.toInt() == 1)
{
result = "$days day $hours hour"
}
else if(hours.toInt() == 0 && days.toInt() ==0)
{
result = ""
}
else if(hours.toInt() > 1 && days.toInt() > 1)
{
result = "$days days $hours hours"
}
return result
【问题讨论】:
标签: android kotlin calendar simpledateformat dateformatter