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.
Related
I'm having fierce problems trying to get DateUtils.getRelativeTimeSpanString to return what it's supposed to in Android.
I've been through every similar question here and none of the solutions posted are working or relevant.
Here's my code:
CharSequence relativeDate =
DateUtils.getRelativeTimeSpanString(System.currentTimeMillis(),
currentNewsItem.getTimeInMilliseconds(),
DateUtils.HOUR_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);
dateView.setText(relativeDate);
The currentNewsItem.getTimeInMilliseconds method does indeed return a long (for example, just now it passed 1521759734 which seems correct) and the getRelativeTimeSpanString passes a correctly formatted date string to the view in the format "March 23, 2018" so it knows the correct date. But I need it to pass "x Hours ago" or "x minutes ago" and I cannot for the life of me figure out why it's not.
I've checked the time on the emulator, and the problem occurs on my device so it's not the timezone settings.
Documentation says that it will revert to a date string like this if the time span is greater than 7 days but I've checked the long that's being passed as the old time and the converter websites shows it's the exact time 4 hours ago. Besides, it's showing the correct date (today), just not the hours/minutes ago.
I've tried the HOUR_IN_MILLIS, MINUTES_IN_MILLIS and SECONDS_IN_MILLIS constants for the third parameter and I've tried removing the last abbreviation parameter and it's still the same result.
I've tried the various public method signatures to try find one that might work but they all have the same result.
Has anyone experienced this or can anyone point out where I'm going wrong?
Many thanks.
If you check DateUtils.getRelativeTimeSpanString, the parameters are listed as:
getRelativeTimeSpanString (long time,
long now,
long minResolution,
int flags)
However in your code,
now: the current time in milliseconds, is passed as 1st parameter instead of currentNewsItem.getTimeInMilliseconds().
To get relative timespan like X minutes ago you can use DateUtils.MINUTE_IN_MILLIS and flag like DateUtils.FORMAT_ABBREV_RELATIVE will abbreviate relative time as X mins. ago.
So, you can change your code to:
CharSequence relativeDate =
DateUtils.getRelativeTimeSpanString(currentNewsItem.getTimeInMilliseconds(),
System.currentTimeMillis(),
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE);
dateView.setText(relativeDate);
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 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.
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.
i have to select a interval of time.
i have two timepickers on my app, i need to check if timepicker1 selected time is less than timepicker2 selected time. If not, i have to show a toast to told the user the error.
I also need to do this with two datepickers,not with times in that case, but with dates.
please can someone give me some code examples for do this?
You just need to compare the individual components of the date or time in the correct order. For time:
if (time1.getCurrentHour() < time2.getCurrentHour() || (time1.getCurrentHour() == time2.getCurrentHour() && time1.getCurrentMinute() < time1.getCurrentMinute())) {
//time 1 is earlier.
}
You might need to add in a bit of complexity depending on if you are showing 24 hour time or not.
For dates, its the same, just compare first the year then the month then the day.