Skip to content

Instantly share code, notes, and snippets.

@0ryant
Created August 6, 2019 11:37
Show Gist options
  • Save 0ryant/d237cf56e98846576651580d3d22efc4 to your computer and use it in GitHub Desktop.
Save 0ryant/d237cf56e98846576651580d3d22efc4 to your computer and use it in GitHub Desktop.
Java - Coding Challenge 9 - Minutes to Years and Days
public class MinutesToYearsDaysCalculator {
public static void printYearsAndDays(long minutes){
if (minutes <0) {
System.out.println("Invalid Value");
} else {
long years = minutes / 525600;
long minutesRemaining = (minutes - (years * 525600));
long daysRemaining = minutesRemaining / 1440;
System.out.println(minutes + " min = " + years + " y and " + daysRemaining + " d");
}
}
}
@kannojsh
Copy link

Wrong program

@mugunthank7
Copy link

im a beginner can someone comment on my model , anyway both are similar af :)
public static void printYearsAndDays(long minutes){ if (minutes<0){ System.out.println("invalid value"); }else { int years = (int)minutes/525600; int days= (int) ( (minutes/1440)-(years*365)); System.out.println(years+" years " +days+ " days "); } }

Copy link

ghost commented Oct 4, 2021

Wrong program

the above program wont work in intellij idea you have to change the void to long and add a return statement if you want it to work
MinutesToYearsDaysCalculator.java
public class MinutesToYearsDaysCalculator {
public static long printYearsAndDays(long minutes) {
if (minutes < 0) {
System.out.println("Invalid Value");
} else {
long years = minutes / 525600;
long minutesRemaining = (minutes - (years * 525600));
long daysRemaining = minutesRemaining / 1440;
System.out.println(minutes + " min = " + years + " y and " + daysRemaining + " d");
}
return minutes;
}
}
main.java
public class main {
public static void main(String[] args) {
long years= MinutesToYearsDaysCalculator.printYearsAndDays(525600);
}
}

@0ryant
Copy link
Author

0ryant commented Oct 5, 2021

Sorry I wrote this years ago, if I posted it then it passed the unit tests 😅

@aiham-lab
Copy link

public static void printYearsAndDays(long minutes) {
    if (minutes < 0) {
        System.out.println("invalid Value");
    } else {
        long days = minutes / 1440;
        long years = minutes / 525600;


        System.out.println(minutes + " min = " + years + " y and " + days + " d");

    }

}

@ilialloyd
Copy link

ilialloyd commented Sep 1, 2022

public class MinutesToYearsDaysCalculator {
    public static void printYearsAndDays(long minutes){
         if (minutes < 0) {
                System.out.println("Invalid Value");
            }else {
// make to understand process easy, I go step by step
                long hour = minutes / 60;    //simple find first hours
                long day = hour / 24;           // then go for days
                long year = day / 365;        //then find a years
                long remaindays= day%365;     //then go for remaining days, so, after some years how many days left we will see

                System.out.println(minutes + " min = " + year + " y and " + remaindays + " d");
            }
    } 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment