简体   繁体   中英

Converting seconds to Year Hour Minutes Seconds

Im new, standard beginning, so I'll leave the rest out. Do I have to convert every int to seconds, or is this right? Because when I calculated backwards, manually and count the seconds together of the final statement, I get differences in hours.

Thx Casu

    Scanner input = new Scanner(System.in);
    final int secmin = 60;
    final int day = 24;
    final int year = 365;
    int seconds = input.nextInt();
    int minutes = seconds / secmin;
    int remainSec = seconds % secmin ;

    int hours = minutes / secmin;
    int remainMin = hours % secmin;
    int days = hours / day;
    int remainHours = hours % day;
    int years = days / year;
    int remainDays = days % year;

    if (minutes <= 59)
    {
        System.out.println(seconds + " seconds is " + minutes +" minutes and " + remainSec + " seconds");
    }
    else if (hours <= 23) 
    {   

        System.out.println(seconds + " seconds is " + hours +" hours and " + remainMin + " minutes " + remainSec + " seconds");
    }
    else if (days <= 364)
    {   

        System.out.println(seconds + " seconds is " + days + " days "  + remainHours +" hours and " + remainMin + " minutes " + remainSec + " seconds");
    }

    else
    {
        System.out.println(seconds + " seconds is " + years + " years " + remainDays + " days "  + remainHours +" hours and " + remainMin + " minutes " + remainSec + " seconds");
    }

I think this statement needs to be changed : From "int remainMin = hours % sec min;"

to " int remainMin = minutes % secmin;".

Rest all looks good.

The code looks good. However, I would remove the "Magic Numbers" from the if statements:

Scanner input = new Scanner(System.in);
final int secmin = 60;
final int day = 24;
final int year = 365;
int seconds = input.nextInt();
int minutes = seconds / secmin;
int remainSec = seconds % secmin ;

int hours = minutes / secmin;
int remainMin = hours % secmin;
int days = hours / day;
int remainHours = hours % day;
int years = days / year;
int remainDays = days % year;

if (minutes < secMin)
{
    System.out.println(seconds + " seconds is " + minutes +" minutes and " + remainSec + " seconds");
}
else if (hours < day) 
{   

    System.out.println(seconds + " seconds is " + hours +" hours and " + remainMin + " minutes " + remainSec + " seconds");
}
else if (days < year)
{   

    System.out.println(seconds + " seconds is " + days + " days "  + remainHours +" hours and " + remainMin + " minutes " + remainSec + " seconds");
}

else
{
    System.out.println(seconds + " seconds is " + years + " years " + remainDays + " days "  + remainHours +" hours and " + remainMin + " minutes " + remainSec + " seconds");
}

I know the year/day/hour/minute/second ratios are all pretty static, but what if someone wanted to see your code with 10 seconds to the minute and 10 minutes to the hour? Then you only need to change one place instead of two.

In a production application this could grow to hundreds of locations within the code and chasing down every single one is difficult.

I did enter=

1234234234 s

output=

1234234234 seconds is 39 years 50 days 2 hours and 2 minutes 34 seconds

Sum=

1229904000 s + 4320000 s + 7200 s + 120 s + 34 s = 1234231354 s

Difference =

2880 s

When I change:

int remainMin = hours % secmin; "to" int remainMin = minutes % secmin;

Then:

1234234234 seconds is 39 years 50 days 2 hours and 50 minutes 34 seconds

Result:

48 min add

1229904000 s + 4320000 s + 7200 s + 3000 s + 34 s = 1234234234 s

and the difference is 0.

Thank you for the correction user2006544 and for the improvement Steve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM