Convert the given time into milliseconds [closed] - java

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

Related

stuck with this while loop java [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 4 years ago.
Improve this question
I have been working with while loops however I cant seem to understand how the remainder works inside this code.
int a = 10;
while( a <= 1000 && a % 100 != 0){
System.out.println("a = " + a);
a = a + 10;
}
a & 100 != 0
Performs bitwise and , then compares the result to 0. It will be false even in the first iteration, since 10 & 100 = 0

Java convert minutes into hours minutes [duplicate]

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 ));

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

Recursive Equations with Time Complexity [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am really struggling with this recursive question. Can anyone help me solve the recurrence T(n) = 5T (n/5)+5 with the base condition T(1) = 0 via closed-form formula? It is given that n = 5^m with the integer m = log5 n.
It will be sufficient to compute T(5n) for n >= 0. For all other values of x, T(x) will equal T(y) where y is the largest power of 5 smaller than x, since the calculations are the same. (I'm assuming that when you write n/5 you mean integer division, i.e. floor(n/5).)
Then:
T(50) = 0
T(51) = 5 * 0 + 5 = 5
T(52) = 5 * 5 + 5 = 52 + 51
T(53) = 5 * (5 * 5 + 5) + 5 = 53 + 52 + 51
... which leads to:
T(5n) = 5n + 5n-1 + ... + 52 + 51
which, using a high-school algebra formula (sum of a geometric series), is
T(5n) = (5n+1 - 5) / 4
If you're thinking about time complexity, notice that T(x) will always be less than or equal to 5x / 4. And since we don't worry about constant factors when expressing things in O-notation, this essentially means T(x) = O(x).
A non constructive way to solve this: looking a bit at the formula one guesses that T(5m) = (5m+1-5)/4. This can be shown by induction:
it is correct for m=0: T(1) = 0
assuming it is correct for m we show it for m+1: T(5m+1) = T(5*5m) = 5T(5m)+5 = 5*((5m+1-5)/4)+5 = (5m+2-25)/4+5 = (5m+2-5)/4.
Therefore it is correct for all m.

Java algorithm to work out time using distance and time? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm looking for an implementation in Java using a method for:
time = distance / speed
Time in minutes
Distance in metres
Speed in kilometres per hour
If you want to measure the time t, taken to reach a distance d with a speed of v,
then
// time = distance / speed
t = d / v units_of_time
For a distance of 60 miles, if you are driving at a speed of 40 mph then
t = 60 / 40
t = 1.5 hours
Java Solution:
long distance = 60; // kilometers
double speed = 40.0d; // kmph
double speed_in_meters_per_minute = ( speed * 1000 ) / 60; // mpm
// now calculate time in minutes
double time = (double)distance / speed_in_meters_per_minute ;
You can round of time value to a desired precision and scale.
Yes. Rate*Time=Distance. So, once you have figure out that distance, just take that, divide it by what ever speed the user entered, and Viola!
Pseudo code:
TimeToTravel = DistanceBetweenPoints / TimeUserEntered
Helpful?
Distance = Rate * Time
Div both sides by Rate:
Time = Distance / Rate (Speed)
speed = distance / time
Therefore:
time = distance / speed
distance in 2d = sqrt((x2 - x1)^2 + (y2 - y1))
then its just distance over time
v = d/t

Categories