【问题标题】:How to change seconds into hours and hours value must not exceed 24 hours.? [closed]如何将秒变为小时,小时值不能超过 24 小时。? [关闭]
【发布时间】:2018-05-06 05:53:50
【问题描述】:

我编写了一个程序来将秒值更改为秒、分钟、小时和天。除小时外,所有其他值都给出了准确的输出。每当用户提供超过 86400 的价值时,它就会提供超过 24 小时的价值,这是我不想要的。 ..

int days = totalSeconds / 86400;
totalSeconds -= days * 86400;
int hours = totalSeconds % 3600;
totalSeconds -= hours * 3600;
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;

【问题讨论】:

  • 你能展示你的代码吗?
  • 您需要将剩余的天数结转
  • 使用Duration...
  • 不允许用户提供高于 86400 的值。一个简单的 If 语句应该可以解决这个问题。
  • 使用Duration,如here所示

标签: java


【解决方案1】:

如果您从 totalSeconds 开始,将 int 分解为秒、分钟、小时和天,则此序列应该在 23 小时达到最大值:

int days = totalSeconds / 86400;
totalSeconds -= days * 86400;
int hours = totalSeconds / 3600;
totalSeconds -= hours * 3600;
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;

【讨论】:

  • 工作就像一个魅力.. 谢谢大家
  • 我只要求一个赞成票。 :)
猜你喜欢
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多