I have timer for a task. And all the sessions will be added up to each other.
Let's say today user spent 5 minutes
Other day he spent 1 hour, here this one hour will be added to the 5 minutes
and so on..
So it will be the total time in one value..
How can I do this ? Is it by Milliseconds or Date object ?
Date is more useful when you are dealing with actual calendar dates.
If you just want to keep track of time intervals/durations, just have a long variable and keep on adding the durations to this.
EDIT : Long.MAX_VALUE is 9,223,372,036,854,775,807. So, you don't really need to worry about the overflow either.
You can keep track of all the milliseconds using Date().getTime(), which returns the time since Epoch. So whenever you need to add/remove, just take your Date object, invoke .getTime(), and add/remove from the total. Then when you're done, you can convert the milliseconds to whatever format you need.
Related
I'm trying to find the difference between 2 times using Joda Time. I've tried using some of the examples on here and can not get a value to return. Here is what I've tried: Where time is in a format like 17:23 and the to and from variables are time zones like America/Los_Angeles or Europe/London
DateTime dtFrom = DateTime.now()
DateTime dtTo = new DateTime(dtFrom.withZone(DateTimeZone.forID(to)));
//calculate time difference
Period period = new Period(dtTo, dtFrom) ;
Minutes minutes = Minutes.minutesBetween(dtFrom, dtTo);
long diffInMillis = dtFrom.getMillis() - dtTo.getMillis();
I've tried using different methods to compare these two and always get no value returned...period returns PT0S, minutes returns PT0M, and diffInMillis returns 0. What am I doing wrong here? Thanks!
You are using the method withZone() which is defined to preserve the (POSIX) instant in milliseconds since 1970-01-01T00:00Z. So dtFrom and dtTo have still the same instant meaning they describe the same global time although their local time representations are different due to different time zones.
Result: The difference between two same instants is exactly equal to zero regardless which time unit you use.
However, if you are interested into the local time difference instead please determine the zone offset difference, for example using the method getOffset().
I am developing small android application. And I want to do something in my application after some minutes. These minutes are not static one these are dynamic ones.
So i am using android calender setInexactRepeating for this.
My code looks like this
Calendar cal = Calendar.getInstance();
// Start something after 4 minutes
cal.add(Calendar.MINUTE, 4);
get_alaram_service().setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 1000*300, get_pendingintent());
So this will work setInexactRepeating after 4 min it will run my pending intent and after that it will keep repeating this for this much amount of time. (1000*300).
So my problem is that in setInexactRepeating 2nd parameter is for at what time I want to start my timer and 3rd parameter for repeating this thing. Now 2nd parameter tales value in milisec. I tried to pass my own value of minutes in milisec like(1000*300) then its not working properly. I don't how its working properly. When I checked cal.getTimeInMillis() it is very big integer number. what is that actually.
Am i doing something wrong need your help thank you...
Although this isn't explicitly stated in the documentation the second parameter (triggerAtMillis) is time in milliseconds since the Epoch. This is what Calendar.getInstance() returns. Calling this method will return the current time. This is a big number, since it is actually the number of milliseconds after 1/1/1970. You then need to add something to it (e.g. 4 minutes) to define when the AlarmManager will first fire.
For a task remainder application i need to compare the mobile current date and time with my task and time.
How this is possible??
convert your task time to long value suppose x1, and get system time via System.currentTimeMillis() sppose x2.
compare them like one compare long values..
Use Calendar class.
Get an instance of Calendar, set hours, minutes, date etc, and then use getTimeInMillis() which you need to compare with System.currentTimeMillis().
Also, you might want to take a look at AlarmManager to actually schedule some action to some moment of time.
I have tried to do this without bothering the experts and have read numerous threads here and on other sites. It is clearly my brain not understanding what needs to be done in order for this to work.
My goal is that the app allows the user to enter a time and one or more days in a week. All of the GUI side and storing of the dates and times I have done, however to get the alarm manager to repeat, lets say every Monday at 14:00 and then can send at 14:02 . I have used the java Calendar object to hold the times and days of the week or even used date and day of the week of the month. These are then , as needed, converted to milliseconds for it to be read in by the alarm manager.
I then have used either the alarm manager set or set repeat methods to repeat the event. All I am able to do is get it to occur once and then if I change the emulator date and time to another Monday nothing happens.
The GUI holds the hours and minutes in required variables and then these are used against the calendar objects.
The alarm manager calls a broadcast receiver for the event to occur.
Please can someone simply give an example on how to set specific days such as Monday , Wednesday Friday. I know that separate alarm managers are needed for each day and at the moment I have just focused on Monday as my main test.
Links viewed:
How can i Repeat the Alarm in android for only Monday, Tuesday and Friday
How to repeat the alarm for "n" days at a particular time
how to repeat alarm after 1 day in android
Managed to figure this out now and so follows my answer:
The following code calculates the remaining days between now and the day needed for the scheduled task. the variable whichday is passed via parameter from the method this code belongs to. In the understanding of this whichday represents days of the week 1 through to 7 where 1 is Sunday , 2 is Monday and so .
//This gets the current day of the week as of TODAY / NOW
int checkcurrentday = getcurtime.get(Calendar.DAY_OF_WEEK);
// This calculates the days between now and the day needed which is represented by whichday.
int numberofdays = Calendar.SATURDAY + whichday - checkcurrentday;
//Now add NOT set the difference of the days to your Calendar object
tMondayOn.add(Calendar.DATE, numberofdays);
Well, you need to first use the Java Calendar API (or Joda!) to figure out when the next monday is. Set the alarm to to that time in milliseconds then use setRepeating and pass in a long that represents the interval of one week.
So I have a scenario:
Using System.currentTimeMillis() I get the initial start time of my application. Then, when I open it at another time, I get the time again. Both of these times are saved as a long variable.
What I need to do is compare these two times, convert this difference to hours (x 600,000), and for every hour that has passed, subtract 5 from another integer variable. I can find the difference in time, convert it to hours, but I can't figure out to subtract 5 for every hour that has passed. Any help or pseudo code would be GREATLY appreciated.
-Nathan
If for every hour we subtract 5, then after {x} hours we will have subtracted 5 * x.
So work out how many hours have passed, multiply it by five, and there you go.