Countdown using CountDownTimer - java

I have a countdown timer. The countdown timer counts down in miliseconds. I have worked out hours by using the sum:
(((millisUntilFinished / 1000) / 60) / 60)
Now minutes, I did this today and I seriously can not see how minutes works, but it does, here is what I have done for minutes...
long minsTotal = hoursS * 60;
(((millisUntilFinished / 1000) / 60) - minsTotal)
Now when I typed that in to a caluculator it awlays equated to 0... But somehow it works....
Now the seconds is where i'm stuck. To work out the seconds I have used:
long secs = (millisUntilFinished / 1000);
which works out the total seconds. However I want the highest number for it to display as 60. How could I stop if from going over 60, but still working properly?

Related

How to countdown to a date in java? [duplicate]

This question already has answers here:
Timer - counting down and displaying result in specific format [duplicate]
(2 answers)
Closed 6 years ago.
So i am creating an application to countdown to a date in java.
So lets say i parse these 2 days:
Date Now = 2016/01/15 16:52:22
Date End = 2016/01/15 18:37:18
How would i calculate the differences so that i can get the output like so:
1 hour, 44 minutes and 56 seconds
instead of the total hours, total minutes and total seconds.
I guess the best way to explain it is that i need to get the seconds left of the minute, the minutes left of the hour, etc, etc.
You haven't specify your date format. Supposed you use the Date class, the following code gives you a difference in milliseconds.
long difference = dateEnd.getTime() - dateNow.getTime();
Then you approach to your result with a simple calculation:
int seconds = (int) (difference / 1000) % 60 ;
int minutes = (int) ((difference / (1000*60)) % 60);
int hours = (int) ((difference / (1000*60*60)) % 24);

using only currentTimeMillis() to get the date

So I'm totaly lost. I have to print out the actual Date and time using only currentTimeMillis() and nothing else. Getting the time was no problem:
long t = java.lang.System.currentTimeMillis();
long seconds = (t / 1000) % 60;
long minutes = (t / (1000 * 60)) % 60;
long hours = (t / (1000 * 60 * 60) % 24) + zone;
But how can I calculate the date taking leap years into consideration?
Edit: It's homework, hence this weird question. And we are not allowed to use any other methodes besides currentTimeMillis(). Operators and alike are fine.
For the timezone related problem, following fix can be used. However this is just one case: other cases need to be handled as well.
//let's say time zone is +5:30
long zone = 6; //instead of 5, keeping value 6, thus added extra 30 minutes
long minuten = (t / (1000 * 60)) % 60;
long stunden = (t / (1000 * 60 * 60) % 24) + zone;
if(minuten < 30){ // this is to take care of the cases where hour has moved ahead
stunden--;
minuten+=30;
} else{
minuten-=30; // else deduct those additional 30 minutes
}
There is no way to do this without using anything else. You have to be more clear about which restrictions there are and why? Is this homework?
The simplest way I can think of is:
long t = java.lang.System.currentTimeMillis();
System.out.println(new Date(t));
But I am sure that's not what you need. Please elaborate...

Convert Minutes to Hours using JodaTime

I'm having Minutes in java.lang.Long and want to convert this value to java.math.BigDecimal, ie. as Hours.
BigDecimal hours = BigDecimal.valueOf(minutes)
.divide(BigDecimal.valueOf(DateTimeConstants.MINUTES_PER_HOUR))
.setScale(2,RoundingMode.HALF_DOWN);
Tried the above method. It return hours, but no the way actually i want it
How i need is :
240 Minutes : 4 Hours
230 Minutes : 3.50 hours
Any help?
I would convert your Minutes to a Period object:
Minutes minutes = ...;
Long millisec = minutes*60*1000;
Period period = new Period(millisec);
Then use the Period object you can ask the Hours. Anything you want...
Note: 230 minutes is not 3.50 hours, it's 3.83 hours, i'm assuming you mean "3 hours and 50 minutes".
So what you want is the hh:mm representation.
You don't need BigDecimals. Use this:
long minutes = 230;
long hours = minutes / 60;
long minnutesRemaining = minutes % 60;
System.out.println(hours + "." + minnutesRemaining);
I'm betting the OP actually wants to convert minutes into hours and minutes. This is as easy as:
int minutes = 230;
System.out.println(
String.format("%d Minutes: %d:%02d Hours", minutes, (minutes/60), (minutes%60)));
Just printing the minutes divided by 60 (using integer arithmetic) and the modulo of minutes divided by 60 (formatted as two digits with leading zeros by the "%02d" format.
You can do this using BigDecimal easy. You can use divideAndRemainder()
long minutes = 230L;
BigDecimal min = new BigDecimal(minutes);
BigDecimal constant = new BigDecimal(60);
BigDecimal[] val=min.divideAndRemainder(constant);
System.out.println(val[0]+"."+val[1]+" Hours");
Out put:
3.50 Hours
I don't know in what universe 230 minutes equals 3.5 hours, so I'm afraid that some string manipulation is your best bet:
BigDecimal hours = new BigDecimal(
String.format("%d.%d", minutes / 60, minutes % 60));
Printing out the value of hours yields 3.50, as per your requirement.
Use integer and modulo arithmetic:
long hours = minutes / 60; /*implicit round-down*/
long numberAfterDecimal = (minutes % 1.0 /*pull out the remainder*/) * 60;
Then format these two numbers as you wish.

how to convert a certain number to hours/minutes?

Hello everyone well i have a certain long number and i wish to divide it and show how many minutes and hours remaining :
timeToReceive = Utils.currentTimeMillis() + (60 * 1000 * 60 * 8); // 8 hours
here is my timeToReceive long.
I want to show how much time is remaining for the timeToReceive(it's set for 8 hours in the future).
So i do this :
(timeToReceive - Utils.currentTimeMillis()) / (1000 * 60)
this displays it in minutes, however i want to display it in hours and minutes, how will i go bout doing that?
thanks.
timeInMinutes = (timeToReceive - Utils.currentTimeMillis()) / (1000 * 60);
hours = timeInMinutes / 60;
minutes=timeInMinutes % 60;
This works fine
First count the time in minutes
minutes = (timeToReceive - Utils.currentTimeMillis()) / (1000 * 60)
Then use / operation and % operation
minutes / 60; // will just divide and truncate - gives you hours
minutes % 60; // will give you the rest that is left after dividing - the part that was truncated when you used / operator

Calculating difference in two time

I am calculating the difference between two times and i am able to get the difference in hours and minuted using separate equations,is there any single equation from which i can get hours and minutes at one go.I am using like:
here diff is the difference between two time value
long diffHours1 = diff / (60 * 60 * 1000);
long min=diff/(60*1000);
I'm not sure it is helpful here, but Joda Time seems to have a little more verbose solution using Period e.g.
Period p = new Period(startDate, endDate)
int minutes = p.getMinutes(); //returns the left over minutes part
int seconds = p.getSeconds(); //returns the seconds part
I'm not sure that for this particular case you need something else than what you have, I agree with aix's
is there any single equation from which i can get hours and minutes at one go
No, not easily. A Java expression can only have one result; returning several things is not impossible, but would require additional machinery (a wrapper class, a tuple etc). This would result in code that's significantly more complicated than what you have right now.
What you can do to simplify things a little bit is compute minutes first, and then compute hours based on minutes:
long diffMinutes = diff / (60*1000);
long diffHours = diffMinutes / 60;
Yes there is:
String oneGo = (diff / (60 * 60 * 1000)) + " " + (diff / (60 * 1000));
:-)
Well, two equations are not that bad (actually using more lines makes it easier to read), although you might change the order, correct the equation and cache some results.
diff = //time difference in milliseconds
long diffInMinutes = diff / (60 * 1000);
//get the number of hours, e.g. 140 / 60 = 2
long hours = diffInMinutes / 60;
//get the left over minutes, e.g. 140 % 60 = 20
long minutes = diffInMinutes % 60;
If the reason you want one equation is ease of use, try using an alternative library like Joda Time.

Categories

Resources