gpt4 book ai didi

java - 将持续时间转换为季度

转载 作者:行者123 更新时间:2023-12-01 04:52:39 25 4
gpt4 key购买 nike

我需要计算季度、周、天、小时和秒的时间差。我无法得到正确的公式。以下公式应返回:3 季度、12 周、6 天、23 小时 59 分钟。我绝不是数学奇才,因此任何帮助都会很棒。

long difference = 31535872360L;/*A few seconds short of a year*/

long min = (int) ((difference / (1000*60))%60);/*Returns 59 Minutes*/
long hours = (int) ((difference / (1000*60*60)) % 24);/*Returns 23 Hours*/
long days = (int) ((difference / (1000*60*60*24)) % 7);/*Returns 0 Days*/
long weeks = (int) ( difference / (1000*60*60*24*7)% 12);/*Returns 4 Weeks*/
long quarters = (int) (((difference / (1000*60*60*24*7)%12)/3)+1);/*Returns 2 Quarters*/

最佳答案

我相信这种方式更干净并且不易出错:

private static final long MIN_MS = 1000 * 60;
private static final long HOUR_MS = MIN_MS * 60;
private static final long DAY_MS = HOUR_MS * 24;
private static final long WEEK_MS = DAY_MS * 7;
private static final long QUARTER_MS = WEEK_MS * 12;

public static void main(String[] args) throws Exception {
long msecs = 31535872360L;/*A few seconds short of a year*/
long q = msecs / QUARTER_MS;
msecs -= q * QUARTER_MS;
long w = msecs / WEEK_MS;
msecs -= w * WEEK_MS;
long d = msecs / DAY_MS;
msecs -= d * DAY_MS;
long h = msecs / HOUR_MS;
msecs -= h * HOUR_MS;
long min = msecs / MIN_MS;
msecs -= min * MIN_MS;
long secs = msecs / 1000;
System.out.printf("%d q,%d w, %d d, %d:%d:%d \n", q, w, d, h, min, secs);
}

关于java - 将持续时间转换为季度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14714747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com