I'm trying to get my app to display a time, and for that I need to get the android's minutes and hours. I'm trying to use currentTimeMillis(), but I'm getting the wrong number for the hours. Here's my code for the hours and minutes using the systems clock.
int defday = (int) (System.currentTimeMillis() / (1000 * 60 * 60 * 24));
int defhour = (int) (System.currentTimeMillis() - (defday * 1000 * 60 * 60 * 24))
/ (1000 * 60 * 60);
int defmin = (int) (System.currentTimeMillis()
- (defhour * 1000 * 60 * 60) - (defday * 1000 * 60 * 60 * 24))
/ (1000 * 60);
After I run the app, the time shows the time as 5:36 even though the current time is 1:36. What am I doing wrong?
Create a Calendar object:
long millis=System.currentTimeMillis();
Calendar c=Calendar.getInstance();
c.setTimeInMillis(millis);
After this you can get the fields from the Calendar object:
int hours=c.get(Calendar.HOUR);
int minutes=c.get(Calendar.MINUTE);
Then:
int MinutesHours=(hours*60)+minutes;
To go back, you can use the set method in Calendar:
Calendar c=Calendar.getInstance();
c.set(Calendar.MINUTE,minutes);
long millis=c.getTimeInMillis();
References
How to measure the a time-span in seconds using System.currentTimeMillis()?
currentTimeMillis() to Years, days, and minutes, and backwords. (hard)
Related
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...
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
I have worked a bit on the following code but still unable to print milliseconds (not all the millisecond from epoch time to user defined time).
Where am I lacking to print remaining milliseconds e.g. If seconds are 30 exactly, milliseconds should be only 0. Milliseconds should not be more than 999 obviously.
// Sets current date by default
Calendar ob = Calendar.getInstance();
// Sets user defined date with year, month, day of month respectively
Calendar dob = Calendar.getInstance();
dob.set(1990, 3, 25);
// Want to get milliseconds only (0 - 999)
long milli = dob.getTimeInMillis() - ( 60 * 60 * 24 * 30 * 12 * ( ob.get(Calendar.YEAR) - dob.get(Calendar.YEAR) ) );
System.out.println(milli);
Why not do
long justMillis = dob.getTimeInMillis() % 1000;
You want what's called the modulus operator, %. This basically finds the remainder of division.
long milli = dob.getTimeInMillis() % 1000;
System.out.println(milli % 1000);
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.
I'm writing a program that asks the user for their birthdate and then calculates their age in years of different planets. I am not suppose to assume how the birthdate is to be entered except that there is one white space between each number.
The code I have right now does not meet these specifications right now and I'm not sure how to write it otherwise. I am also having problem calculating what my age would be today. When I enter my birthdate and print out age, it currently tells me that I'm 407 yet when I print out dateBirth and today, both of those dates are correct.
System.out.print("Please enter your birthdate (mm dd yyyy): ");
birthdate = scan.nextLine();
DateFormat df = new SimpleDateFormat("MM dd yyyy");
Date dateBirth = df.parse(birthdate);
Calendar calBirth = new GregorianCalendar();
calBirth.setTime(dateBirth);
Calendar calDay = new GregorianCalendar();
today = calDay.getTime();
age = (today.getTime() - dateBirth.getTime()) / (1000 * 60 * 60 * 24 * 365);
1000 * 60 * 60 * 24 * 365 is actually 31536000000 which is bigger than Integer.MAX_VALUE this causes an overflow. As an integer it would be evaluated to 1471228928 which leads to the wrong result.
The solution is append the letter L to one of your constants
long div = ( 1000 * 60 * 60 * 24 * 365L );
long age = ( today.getTime() - dateBirth.getTime() ) / div;
You should check if the expression 1000 * 60 * 60 * 24 * 365 evaluates to the result you are expecting and if not, find a way to get the expected result. Perhaps you should even consider that on earth, we have so called leap years and that you could tag your question as homework.
1000 * 60 * 60 * 24 * 365
Is an int, but its to long to hold it. Make one of these a long, like:
1000L * 60 * 60 * 24 * 365