my problem is that i am unable to get the days correctly.
help me out!
here is my code.
package basic.programs;
import java.util.Scanner;
public class MinutesToYearDaysConversion {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int Minutes, Years, MinutesRemain, Days, MinutesRemainAfterDays, Hours, MinutesRemainAfterHours;
System.out.print("give the minutes : ");
Minutes = sc.nextInt();
Years = Minutes / 535600;
MinutesRemain = Minutes % ( 535600);
Days = MinutesRemain / 1440;
MinutesRemainAfterDays = MinutesRemain % (1440 );
Hours = MinutesRemainAfterDays / 60;
MinutesRemainAfterHours = MinutesRemainAfterDays % (60);
System.out.print("Given minutes has" + Years + "years" + Days + "days" + Hours + "hours and " + MinutesRemainAfterHours + "minutes ");
}
}
output :-
give the minutes : 3000010
Given minutes has 5 years 223 days14 hours and 50 minutes
// Write a Java program to convert minutes into the number of years, days, hours and minute.
// Ex : Input : minutes = 3000010 Output : Given minutes has 5 years 258 days 8 hours and 10 minutes
i tried to input the same minutes that the problem gave as an example to check if my code runs correctly or not.
so where am i missing.
That is a lot of magic numbers for functionality that Java includes in TimeUnit. Unfortunately, it doesn't provide a toYears() (and your math assumes one year is exactly 365 days). Anyway, that could be done with something like
Scanner sc = new Scanner(System.in);
System.out.print("give the minutes : ");
int origMinutes = sc.nextInt();
long days = TimeUnit.MINUTES.toDays(origMinutes);
long years = days / 365;
days -= years * 365;
long hours = TimeUnit.MINUTES.toHours(origMinutes
- TimeUnit.DAYS.toMinutes(years * 365)
- TimeUnit.DAYS.toMinutes(days));
long minutes = origMinutes
- TimeUnit.DAYS.toMinutes(years * 365)
- TimeUnit.DAYS.toMinutes(days)
- TimeUnit.HOURS.toMinutes(hours);
System.out.printf("Given minutes has %d years %d days %d hours and %d minutes%n",
years, days, hours, minutes);
Running with your sample input, yields your sample output.
give the minutes : 3000010
Given minutes has 5 years 258 days 8 hours and 10 minutes
In one year there are 525600 minutes, not 535600.
Years = Minutes / 525600;
MinutesRemain = Minutes % ( 525600);
I recommend you do it using java.time.Duration. I also recommend you follow Java Naming Conventions e.g. your variables, Minutes, Years, MinutesRemain etc. should be named as minutes, years, minutesRemain etc.
import java.time.Duration;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter minutes: ");
int mins = sc.nextInt();
Duration d = Duration.ofMinutes(mins);
long days = d.toDaysPart();
long hours = d.toHoursPart();
long minutes = d.toMinutesPart();
System.out.print("Given minutes has " + days / 365 + " years " + days % 365 + " days " + hours + " hours and "
+ minutes + " minutes.");
}
}
A sample run:
Enter minutes: 3000010
Given minutes has 5 years 258 days 8 hours and 10 minutes.
Learn more about the modern date-time API at Trail: Date Time.
Related
I am writing a program to convert minutes into the time of day. How can I write this to keep the hours in standard time, the hour should never exceed 12.
import java.util.*;
public class MinutesConverter {
public static void main(String[] args) {
int minutes;
int hours;
Scanner input = new Scanner(System.in);
System.out.print("Enter minutes: ");
minutes = input.nextInt();
hours = minutes / 60;
minutes %= 60;
System.out.print(hours + ":" + minutes);
}
}
You can make a little change in code, where you are assigning value to hour
here is what i can suggest:
hour = ( minutes / 60 ) % 24;
In the textbook it explains how to convert seconds into minutes with how many seconds remain but the question I have is as follows.
Write a program that prompts the user to enter the minutes (e.g., 1
billion), and displays the number of years and days for the minutes.
For simplicity, assume a year has 365 days. Here is an example.
Enter the number of minutes: 100000000
100000000 minutes is approximately 1902 years and 214 days.
What I currently have is as follows:
import java.util.Scanner;
public class Ch2_Exercise2_7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt user for number of minutes
System.out.println("Enter the number of minutes:");
int minutes = input.nextInt();
// Number of minutes in a year
int year = minutes / 525600;
int day = minutes / 1440;
int remainingMinutes = day % 525600;
System.out.println(minutes + " minutes is " + year + " years and " + remainingMinutes + " days ");
}
}
With what I have it isn't giving me the remaining minutes into days. For example, if I put in 525600 minutes it gives me 1 year and 365 days when it should just be 1 year.
I'm using Java Eclipse. Any help would be greatly appreciated! My apologies in advance if I post the code incorrectly.
You screwed up a bit over here:
// Number of minutes in a year
int year = minutes / 525600;
int day = minutes / 1440;
int remainingMinutes = day % 525600;
You took the total number of minutes and divided by 1440, so the number of days you got was wrong. You should have taken the remainder and then divided by 1440.
Another thing was in your print statement. You wrote the number of minutes left after one year as the number of days.
This should work:
// Number of minutes in a year
int year = minutes / 525600;
int remainingMinutes = minutes % 525600;
int day = remainingMinutes / 1440;
System.out.println(minutes + " minutes is approximately " + year + " years and " + day + " days.");
My main method is separate:
public class MinutesToYearsDaysCalculator {
public static void printYearsAndDays(long minutes) {
double days = minutes / 60 / 24;
double years = days / 365;
double remainingDays = days % 365;
System.out.println((minutes < 0 ? "Invalid Value" : minutes + " min = " + (int) years + " y and " + (int) remainingDays + " d"));
}
}
int mins,hours,remainingDays,days,years;
cout << "Enter minutes:";
cin >> mins;
hours = mins/60;
days = hours/24;
years = days/365;
remainingDays = days % 365;
cout << years << " years " << remainingDays << " days " << endl;
return 0;
public class MinutesToYearsDaysCalculator {
public static void printYearsAndDays(long minutes)
{
if(minutes<0) System.out.println("Invalid Value");
else
{
long hours = minutes/60;
long day = hours/24;
long years = day/365;
long remainingDays = day % 365;
System.out.println(minutes +" min = "+ years+" y and "+remainingDays +" d");
}
}
import java.util.Scanner;
public class Prog13 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter The Minute");
int min=sc.nextInt();
int year= min/(365*24*60);
int remaining_min=(min%24*60*60);
int day=remaining_min/24*60;
System.out.println( min + " " + " Minutes is approximately:"+ year + " " +"year"+ ' '+ day + "Day" );
}
}
hello you can use this result
import java.util.Scanner;
public class MinToYearsAndDays {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("number of minutes:");
int minutes = input.nextInt();
int year = minutes / 525600;
int remainingMinutes = minutes % 525600;
int day = (remainingMinutes / 1440);
System.out.println(minutes + " minutes is approximately " + year + " years and " + day + " days ");
}
}
public class Mints_to_days_years {
// Given some numbers of minutes calculate the number of days and years
// from the given minutes.
// 1hr = 60 minutes
// 1 day = 24 hours
// 1 year = 365 days
public static void find_days_years(long minutes) {
if(minutes < 0) {
System.out.println("Invalid number of minutes.");
}
// to find the number of years, divide the nminutes by total numbers of minutes in a year.
long years = minutes / (60 * 24 * 365);
// then find the remainder minutes that does not goes exactly into year
long years_remainder = minutes % (60 * 24 * 365);
// divide the remainder by number of minutes in a day to find the numbers of days
long days = years_remainder / (60 * 24);
System.out.println(minutes + " mints = " + years + " yrs and " + days + " days.");
}
public static void main(String[] args) {
find_days_years(525600);
find_days_years(1051200);
find_days_years(561600);
}
}
This question already has answers here:
Convert seconds value to hours minutes seconds?
(22 answers)
Closed 6 years ago.
The task is:
The output should look like this (it is a good idea to echo back the input): You entered 500,000 seconds, which is 5 days, 18 hours, 53 minutes and 20 seconds. (5 days 18:53:20 hours)
How should I do it? What is the easiest way to understand and do it?
Also the instructor said "no hard coding" which I'm not exactly sure what is, but I think he wants us to assign them constants.
An example using the built-in TimeUnit.
long uptime = System.currentTimeMillis();
long days = TimeUnit.MILLISECONDS
.toDays(uptime);
uptime -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS
.toHours(uptime);
uptime -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS
.toMinutes(uptime);
uptime -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS
.toSeconds(uptime);
With basic Java arithmetic calculations:
First consider the following values:
1 minute = 60 seconds
1 hour = 3600 seconds (60 * 60)
1 day = 86400 second (24 * 3600)
First divide the input by 86400. If you you can get a number greater than 0, this is the number of days.
Again divide the remained number you get from the first calculation by 3600. This will give you the number of hours.
Then divide the remainder of your second calculation by 60 which is the number of minutes
Finally the remained number from your third calculation is the number of seconds
The code snippet is as follows:
int input = 500000;
int numberOfDays;
int numberOfHours;
int numberOfMinutes;
int numberOfSeconds;
numberOfDays = input / 86400;
numberOfHours = (input % 86400) / 3600 ;
numberOfMinutes = ((input % 86400) % 3600) / 60
numberOfSeconds = ((input % 86400) % 3600) % 60;
It should be like:
public static void calculateTime(long seconds) {
int day = (int)TimeUnit.SECONDS.toDays(seconds);
long hours = TimeUnit.SECONDS.toHours(seconds) - (day *24);
long minute = TimeUnit.SECONDS.toMinutes(seconds) -
(TimeUnit.SECONDS.toHours(seconds)* 60);
long second = TimeUnit.SECONDS.toSeconds(seconds) -
(TimeUnit.SECONDS.toMinutes(seconds) *60);
System.out.println(
"Day " + day + " Hour " + hours + " Minute " + minute +
" Seconds " + second);
}
Explanation:
TimeUnit.SECONDS.toHours(seconds) will give you direct conversion from seconds to hours with out consideration for days. Minus the hours for days you already got i.e., day24*. You now got remaining hours.
The same for minute and second. You need to subtract the already got hour and minutes, respectively.
The simplest way:
Scanner in = new Scanner(System.in);
System.out.println("Enter seconds ");
int s = in.nextInt();
int sec = s % 60;
int min = (s / 60) % 60;
int hours = (s / 60) / 60;
System.out.println(hours + ":" + min + ":" + sec);
Have a look at the class:
org.joda.time.DateTime
This allows you to do things like:
old = new DateTime();
new = old.plusSeconds(500000);
System.out.println("Hours: " + (new.Hours() - old.Hours()));
However, your solution probably can be simpler:
You need to work out how many seconds in a day, divide your input by the result to get the days, and subtract it from the input to keep the remainder.
You then need to work out how many hours in the remainder, followed by the minutes,
and the final remainder is the seconds.
This is the analysis done for you, and now you can focus on the code.
You need to ask what s/he means by "no hard coding". Generally it means pass parameters, rather than fixing the input values. There are many ways to do this, depending on how you run your code. Properties are a common way in Java.
You should try this
import java.util.Scanner;
public class Time_converter {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int seconds;
int minutes;
int hours;
System.out.print("Enter the number of seconds: ");
seconds = input.nextInt();
hours = seconds / 3600;
minutes = (seconds % 3600) / 60;
int seconds_output = (seconds % 3600) % 60;
System.out.println("The time entered in hours,minutes and seconds is:");
System.out.println(hours + " hours: " + minutes + " minutes: " + seconds_output + " seconds");
}
}
You can use the Java enum TimeUnit to perform your math and avoid any hard-coded values. Then we can use String.format(String, Object...) and a pair of StringBuilder(s) as well as a DecimalFormat to build the requested output. Something like,
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number of seconds: ");
String str = scanner.nextLine().replace("\\,", "").trim();
long secondsIn = Long.parseLong(str);
long dayCount = TimeUnit.SECONDS.toDays(secondsIn);
long secondsCount = secondsIn - TimeUnit.DAYS.toSeconds(dayCount);
long hourCount = TimeUnit.SECONDS.toHours(secondsCount);
secondsCount -= TimeUnit.HOURS.toSeconds(hourCount);
long minutesCount = TimeUnit.SECONDS.toMinutes(secondsCount);
secondsCount -= TimeUnit.MINUTES.toSeconds(minutesCount);
StringBuilder sb = new StringBuilder();
sb.append(String.format("%d %s, ", dayCount, (dayCount == 1) ? "day"
: "days"));
StringBuilder sb2 = new StringBuilder();
sb2.append(sb.toString());
sb2.append(String.format("%02d:%02d:%02d %s", hourCount, minutesCount,
secondsCount, (hourCount == 1) ? "hour" : "hours"));
sb.append(String.format("%d %s, ", hourCount, (hourCount == 1) ? "hour"
: "hours"));
sb.append(String.format("%d %s and ", minutesCount,
(minutesCount == 1) ? "minute" : "minutes"));
sb.append(String.format("%d %s.", secondsCount,
(secondsCount == 1) ? "second" : "seconds"));
System.out.printf("You entered %s seconds, which is %s (%s)%n",
new DecimalFormat("#,###").format(secondsIn), sb, sb2);
Which, when I enter 500000, outputs the requested (manual line break added for post) -
You entered 500,000 seconds, which is 5 days, 18 hours,
53 minutes and 20 seconds. (5 days, 18:53:20 hours)
I started doing some pseudocode and came up with this:
import java.util.Scanner;
public class Project {
public static void main(String[] args) {
// Variable declaration
Scanner scan = new Scanner(System.in);
final int MIN = 60, HRS = 3600, DYS = 84600;
int input, days, seconds, minutes, hours, rDays, rHours;
// Input
System.out.println("Enter amount of seconds!");
input = scan.nextInt();
// Calculations
days = input/DYS;
rDays = input%DYS;
hours = rDays/HRS;
rHours = rDays%HRS;
minutes = rHours/MIN;
seconds = rHours%MIN;
// Output
if (input >= DYS) {
System.out.println(input + " seconds equals to " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds");
}
else if (input >= HRS && input < DYS) {
System.out.println(input + " seconds equals to " + hours + " hours " + minutes + " minutes " + seconds + " seconds");
}
else if (input >= MIN && input < HRS) {
System.out.println(input + " seconds equals to " + minutes + " minutes " + seconds + " seconds");
}
else if (input < MIN) {
System.out.println(input + " seconds equals to seconds");
}
scan.close();
}
I've tried mutliple solutions to this problem but I can't seem to get it.
I have time in decimal format, which is in hours. I want to make it much cleaner by changing it into a DD:HH:MM:SS format.
Example:
10.89 hours == 10 hours, 53 minutes, 40 seconds
EDIT: 10.894945454545455 == 10 hours, 53 minutes, 40 seconds
What I've tried:
int hours = (int) ((finalBuildTime) % 1);
int minutes = (int) ((finalBuildTime * (60*60)) % 60);
int seconds = (int) ((finalBuildTime * 3600) % 60);
return String.format("%s(h) %s(m) %s(s)", hours, minutes, seconds);
Which returned: 0(h) 41(m) 41(s)
Any suggestions?
There is no need to do a modular on minutes.
Your calculation of minutes should just multiply by 60, not (60*60)
double finalBuildTime = 10.89;
int hours = (int) finalBuildTime;
int minutes = (int) (finalBuildTime * 60) % 60;
int seconds = (int) (finalBuildTime * (60*60)) % 60;
System.out.println(String.format("%s(h) %s(m) %s(s)", hours, minutes, seconds));
This code gives you the correct output
10(h) 53(m) 24(s)
I believe your expected output of 40 seconds is incorrect. It should be 24 seconds.
(53*60 + 24)/(60*60) = 0.89
Here is a complete implementation:
package test.std.java;
public class Test {
public static void main(String[] args) {
System.out.println(GetReadableTime(10.89));
}
//Prints outs HH:MM:SS
public static String GetReadableTime(double finalBuildTime){
int hours = (int) Math.floor(finalBuildTime);
int remainderInSeconds = (int)(3600.0* (finalBuildTime - Math.floor(finalBuildTime)) );
int seconds = remainderInSeconds % 60;
int minutes = remainderInSeconds / 60;
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
}
First part of rgettman is true :
float finalBuildTime = 10.89;
int hours = (int) finalBuildTime; // 10 ok
But (int) ((10.89 * (60 *60)) / 60) = 683 which is not what you want : it is the direct conversion of finalBuildTime in minutes
int minutes = (int) ((finalBuildTime - hours) * 60); // 53 ok
int seconds = (int) ((finalBuildTime - hours) * 3600 - minutes * 60 + 0.5); // 24 ok
I've added 0.5 for the seconds computation to round to the nearest second rather than truncate. For your example it is no use because your time is an integer number of seconds.
And the number of seconds is 24 seconds = 0.4 minutes
I assume finalBuildTime is a double. You just have to do :
int hours = (int)finalBuildTime;
int minutes = (int)((finalBuildTime - hours) * 60);
int seconds = (int)((((finalBuildTime - hours) * 60) - minutes ) * 60);
(int) rounds down. Therefore, (int)(finalBuildTime) gives you hours.
Since 60 times 60 is 3600, your seconds and minutes calculations are the same.
To find the minutes and seconds, I would observe that the 10.89 hours in your example is 10 hours and 0.89 hours. I would first calculate how many minutes is in 0.89 hours.(the part after the decimal point) That would be 0.89 times 60. Again, since this number should always be less than 60, casting(therefore, rounding down) the number to an (int) gives you minutes. Therefore, for minutes, it is int minutes = (int)((finalBuildTime-hours)*60);
You can keep the rest of your code.
The other answers are correct, but in case it helps you (or someone else who stumbles across this) it is a lot easier to use a library like Joda-Time to do this. Here is a solution to your problem using Joda-Time 2.3:
double buildDurationInHours = 10.89;
long buildDurationInMilliseconds = new Double(DateTimeConstants.MILLIS_PER_HOUR * buildDurationInHours).longValue();
Period period = new Period(buildDurationInMilliseconds);
System.out.println("The build took "
+ period.getDays() + " days, "
+ period.getHours() + " hours, "
+ period.getMinutes() + " minutes, and "
+ period.getSeconds() + " seconds.");
Output:
The build took 0 days, 10 hours, 53 minutes, and 24 seconds.
This way you don't have to deal directly with calculating the hours, minutes, seconds, etc.
Here is my code:
public class numberOfYears {
public static void main(String args[]){
String minsString = JOptionPane.showInputDialog("Enter numbers of minutes:");
double mins = Double.parseDouble(minsString);
//calcuate mins in a year
double minsOfaYear = 365*24*60;
double total = mins / minsOfaYear;
double year = total / 10;
double day = total / 10;
System.out.println(year+ " years and " + day + " days");
//result isnt right
}
}
The question is basically asking to enter minutes and the program supposedly to calculate it to years and days.. for instance, if I enter 1000000000 minutes it should give a result of 1902 years and 214 days.. but I am not getting the correct result. Can someone point out the problem for me please? Thank you!
You should convert the remainders of time to the smaller units of time. In example remainder of years should be converted to days, and remainder of days should be converted to hours etc.
Could you please try this:
public class numberOfYears {
public static void main(String args[]){
String minsString = JOptionPane.showInputDialog("Enter numbers of minutes:");
int mins = Int.parseInt(minsString, 16);
String time = ConvertTime(mins);
System.out.println(time);
}
}
// Convert minutes to years, days, hours an minutes
public String ConvertTime(int time){
String result = "";
int years = time/(365*24*60);
int days = (time%(365*24*60))/(24*60);
int hours= (time%(365*24*60)) / 60;
int minutes = (time%(365*24*60)) % 60;
result = years+" years " + days + " days "+ hours + " hours " + minutes + " minutes";
return result;
}
public class numberOfYears {
public static void main(String args[]){
String minsString = JOptionPane.showInputDialog("Enter numbers of minutes:");
double mins = Double.parseDouble(minsString);
//calcuate mins in a year
double minsOfaYear = 365*24*60;
double year = mins / minsOfaYear;
double day = (mins % minsOfaYear)/(24*60);
System.out.println((int)year+ " years and " + (int)day + " days");
}
}
For this kind of math problem, we need to make use of integer division. If the number of minutes works out to be 45.8 years, you want this to be calculated as 45 years and then convert the remainder of 0.8 years into days. Its not clear to me why your code has a / 10
Try this:
public class numberOfYears {
static double minsOfaYear = 365*24*60;
public static void main(String args[]) {
String minsString = JOptionPane.showInputDialog("Enter numbers of minutes:");
double mins = Double.parseDouble(minsString);
//calcuate mins in a year
int year = (int) (mins / minsOfaYear);
double day = mins - year*minsOfaYear;
System.out.println(year+ " years and " + day + " days");
}
}
I would do it this way
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c2.add(Calendar.MINUTE, 1000000000);
int year1 = c1.get(Calendar.YEAR);
int day1 = c1.get(Calendar.DAY_OF_YEAR);
int year2 = c2.get(Calendar.YEAR);
int day2 = c2.get(Calendar.DAY_OF_YEAR);
int years;
int days;
if (day2 > day1) {
years = year2 - year1;
days = day2 - day1;
} else {
c2.add(Calendar.YEAR, -1);
years = year2 - year1 - 1;
days = c2.getActualMaximum(Calendar.DAY_OF_YEAR) - day1 + day2;
}
System.out.println(years + " years " + days + " days from now");
output
1901 years 119 days from now