计算毫秒到时间的时间

时间:2014-05-02 02:46:50

标签: java time logic

以下代码用于格式化毫秒。计算结果很远。我需要一些帮助。我的代码输出低于Date类的毫秒数。

public static void main(String[] args) {
    System.out.println("Wrong Time: " + getTime(999999999 * 599));

}

//Being called into this method is milliseconds = 598999999401
public static String getTime(long miliseconds) {
    int years = (int) ((miliseconds / (1000*60*60*24*7*52*12)));
    int months = (int) (miliseconds / (1000*60*60*24*7*52) % 12);
    int weeks = (int) ((miliseconds / (1000*60*60*24*7)) % 52);
    int days = (int) ((miliseconds / (1000*60*60*24)) % 7);
    int hours = (int) ((miliseconds / (1000*60*60)) % 24);
    int minutes = (int) ((miliseconds / (1000*60)) % 60);
    int seconds = (int) (miliseconds / 1000) % 60;
    Date date = new Date(598999999401L);//This gets the real time
    System.out.println("Right Time: " + date.getYear() + " years " + (int)(date.getMonth() % 12) + " months " + (int)(date.getDay() % 52) + " weeks "
            + (int)(date.getDay() % 7) + " days " + (int)(date.getHours() % 24) + " hours " + (int)(date.getMinutes() % 60) + " minutes " +
            + (int)(date.getSeconds() % 60) + " seconds");
    return (years <= 0 ? "" : years + " year" + (years != 1 ? "s" : "")) +
            (months <= 0 ? "" : " " + months + " month" + (months != 1 ? "s" : "")) +
            (weeks <= 0 ? "" : " " + weeks + " week" + (weeks != 1 ? "s" : "")) +
            (days <= 0 ? "" : " " + days + " day" + (days != 1 ? "s" : "")) +
            (hours <= 0 ? "" : " " + hours + " hour" + (hours != 1 ? "s" : "")) + 
            (minutes <= 0 ? "" : " " + minutes + " minute" + (minutes != 1 ? "s" : "")) +
            (seconds <= 0 ? "" : " " + seconds + " second" + (seconds != 1 ? "s" : ""));
}

正确输出(日期类)

Right Time: 88 years 11 months 6 weeks 6 days 14 hours 53 minutes 19 seconds

输出错误(我的方法)

Wrong Time:  1 month 3 weeks 2 days 3 hours 25 minutes 45 seconds

更新1(新计算)(仍有逻辑错误):

int years = (int) ((miliseconds / (1000*60*60*24*7*4*12)));
int months = (int) (miliseconds / (1000*60*60*24*7*4) % 12);
int weeks = (int) ((miliseconds / (1000*60*60*24*7)) % 4);
int days = (int) ((miliseconds / (1000*60*60*24)) % 7);
int hours = (int) ((miliseconds / (1000*60*60)) % 24);
int minutes = (int) ((miliseconds / (1000*60)) % 60);
int seconds = (int) (miliseconds / 1000) % 60;

2 个答案:

答案 0 :(得分:2)

两个问题。

您的代码似乎反映了一个月内有52周的信念。这两条线都是错误的。

int years = (int) ((miliseconds / (1000*60*60*24*7*52*12)));
int months = (int) (miliseconds / (1000*60*60*24*7*52) % 12);

此外,您正在使用int您应该使用long的位置。最大int毫秒数不到25天,因此您永远不能使用int进行日期/时间算术。

答案 1 :(得分:0)

一个问题是int溢出,因为你的main方法是使用int文字,而不是long。尝试:

System.out.println("Wrong Time: " + getTime(999999999L * 599L));

另外,请勿使用魔术数字。使用有意义的常量并使代码自我评论:

private static final int MILI_PER_SEC = 1000;
private static final int SEC_PER_MIN = 60;
private static final int MIN_PER_HR = 60;
private static final int HR_PER_DAY = 24;
private static final int DAYS_PER_YR = 365;

然后,

int years = (int) (miliseconds / (MILI_PER_SEC * SEC_PER_MIN * MIN_PER_HR * 
                  HR_PER_DAY * DAYS_PER_YR));

此外,你的计算都是关闭的。您根本不应该在年度计算中使用周数。使用常量,让它们有意义。你怎么知道“正确的”结果实际上是正确的?您正在使用已弃用的代码,并且Date不会像您认为的那样使用毫秒。它根据Date API计算相对于格林威治标准时间1970年1月1日00:00:00的日期。