Java convert minutes into hours minutes [duplicate] - java

This question already has answers here:
How to convert minutes to Hours and minutes (hh:mm) in java
(15 answers)
Closed 8 years ago.
I need to convert minutes to hours and minutes in java, so i do this
java:
long minutes = offer.getDuration();
long hours = minutes / 60;
long minnutesRemaining = minutes % 60;
trainOffer.setDuration(hours+"h"+minnutesRemaining);
output:
minutes = 129
hours = 2
minnutesRemaining = 9
how can i do for have minnutesRemaining = 09 ?

Use String.format to format the output:
System.out.println(String.format("%02d", minnutesRemaining ));

Related

double d = 1000 / 3600; result in to a 0? [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
I doing a simple calculation from 2 variable and I got in to an issue...
In fact when I try to do " double d = 1000 (which is the first var) / 3600 (which is the 2nd var); it result in a 0. So why ? Any hint about that ?
1000 and 3600 are ints, so when you do 1000 / 3600 you get 0. Then, you are assigning double d to this result of zero. You can instead write 1000.0/3600.0 or if these two numbers are variables, you can cast them to doubles first.

Java delete numbers behind comma

I want to calculate Days, Hours etc.
I want to make it like this:
184 Seconds / 60 = 3,0666666666667
Means 3 Minutes.
0,666666666667 * 60 = 4
So 184 Seconds are 3 Min. und 4 Seconds.
Now i dont know how to bring this into Java. I need a function to seperate the Pre-Comma Value from the After-Comma Value.
It's just a simple example. I want to do this with years,weeks,days and so on
It seems that you are looking for modulo (reminder) operator %. Also there is no "after comma value" in integers words so 184 / 60 = 3 not 3.06666.
int time = 184;
int minutes = time / 60;
int seconds = time % 60;
System.out.println(minutes + " minutes : " + seconds + " seconds");
Output: 3 minutes : 4 seconds
You can also use Period from JodaTime library.
int time = 184;
Period period = new Period(time * 1000);//in milliseconds
System.out.printf("%d minutes, %d seconds%n", period.getMinutes(),
period.getSeconds());
which will print 3 minutes, 4 seconds.
Just use %, /, and a little math:
int totalSeconds = 184;
int minutes = totalSeconds/60; //will be 3 minutes
int seconds = totalSeconds%60; // will be 4 seconds

java giving .9999999 instead of a rounded whole number [duplicate]

This question already has answers here:
Floating point arithmetic not producing exact results [duplicate]
(7 answers)
Closed 9 years ago.
Here is the extracted code :
long timeMs = 1473;
double timeS = (timeMs / 1000) + (timeMs% 1000) / 1000.0;
System.out.println(timeS);
And the output is:
1.4729999999999999
So basically, I was just trying to convert the time taken in seconds into milliseconds.
After I saw this I thought my method is wrong, so I've tried other inputs, such as 1472, 1474, 1173, 3 etc which all gave the correct values(1.472, 1.474, 1.173, 0.003).
I think I've came across something similar to this a while ago in a book called Java Puzzlers, but have forgotten. Can anyone tell me why this is happening (and a proper term/error)?
Thanks.
Use this instead, but it's because of IEEE 754 rounding rules.
double timeS = (timeMs / 1000.0); //+ ((double) (timeMs % 1000) / 1000.0);
Use BigDecimal for more accurate rounding and scaling.
long timeMs = 1473;
double timeS = (timeMs / 1000d);
BigDecimal usefulName = new BigDecimal(timeS).setScale(3, RoundingMode.HALF_UP);
System.out.println(usefulName);

How to display exact date difference in php [duplicate]

This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 8 years ago.
My Question is Little bit tricy .
i have to display date difference. (i find out date difference in terms of 2 year 7 month 3 days 5 hrs 30 min.)
Now how i display upto exact 2 higher values
please consider given case
case 1 : date difference is 0 year 2 month 21 days 7 hrs 30 min
output must be : 2 Month 21 days
case 2 : 0 year 0 month 0 days 7 hrs 20 min
output must be : 7 hours 21 days
If your date differences is in the exact form as in your question (separated with " ") and formatted as strings. This will do the trick.
<?php
function display_times($string){
$pieces = explode(" ",$string);
$num_disp = 0;
foreach($pieces as $i => $pice){
if(is_numeric($pice) && intval($pice) != 0){
echo $pice." ".$pieces[$i+1]." ";
$num_disp++;
if($num_disp >= 2) break;
}
}
}
$case1 = "0 year 2 month 21 days 7 hrs 30 min";
$case2 = "0 year 0 month 0 days 7 hrs 20 min";
display_times($case1);
echo PHP_EOL;
display_times($case2);
?>
You can modify this function. Replace 3rd parameter with $depth and modify array_slice line, like on demo.
Use examples :
echo time_diff_string('2013-05-01 00:22:35', 'now', 1), "\n";
echo time_diff_string('2013-05-01 00:22:35', 'now', 2), "\n";
echo time_diff_string('2013-05-01 00:22:35', 'now', 3), "\n";
echo time_diff_string('2013-05-01 00:22:35', 'now', 4), "\n";
Output :
6 months ago
6 months, 6 days ago
6 months, 6 days, 12 hours ago
6 months, 6 days, 12 hours, 6 minutes ago
Demo.

Convert the given time into milliseconds [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to convert the time (hh:mm:ss) into milliseconds. How could i do it?
Is it right to use the following one?
hh * mm * ss * 1000;
But if the minute is 00 or second is 00, it gives 0.
How do I calculate this?
Try this,
hrsSec=hh*60*60;// for seconds;
minSec=mm*60;// for seconds;
secSec=ss;// already seconds;
totalSec=(hrsSec+minSec+secSec);// total seconds
milliSec=totalSec*1000; // milliseconds
Also if you understand the procedure then comment of #Peter Lawrey is noticeable and in short you can try this,
milliSec=((hh × 60 + mm) × 60 + ss) × 1000
(hh * 3600 + mm * 60 + ss) * 1000;
1h == 60min
1min == 60sec
1sec == 1000ms
(ss + (mm + (hh * 60) ) * 60 ) * 1000

Categories