Android/Java - Test if date was last month -
i working on app store info between each use, info boils downwards counting number of times event has happened today, week, month , in apps lifetime. store info in 4 distinct counters can load/save using sharedpreferences.
alongside info store "last run time" of app date, plan during load time load in counters test stored date against today's date determine counters need cleared.
sounds simple right!
after pulling hair out while , going backward , forwards through calendar documentation think understand them plenty come following:
calendar lastly = calendar.getinstance(); last.settimeinmillis(lastdate); calendar today = calendar.getinstance(); today.add(calendar.date, -1); if ( !last.after(today) ) { today = 0; } today.add(calendar.week_of_month, -1); today.set(calendar.day_of_week, calendar.sunday); if ( !last.after(today) ) { today = 0; week = 0; } today = calendar.getinstance(); today.add(calendar.month, -1); today.set(calendar.date, today.getactualmaximum(calendar.date)); if ( !last.after(today) ) { today = 0; week = 0; month = 0; }
i think should fine, issue have testing, testing today easy, testing month logic require either waiting month, or writing test case uses calendar api simulate old date, can't write test case if assumptions on how api works wrong in first place!
therefore, after big wall of text question is... above block of code sane, or have mis-understood working dates in java?
thanks!
edit:second pass @ code:
does more sensible? if understanding things correctly attempting compare end of date lastly saved start of today, week , month.
calendar lastly = calendar.getinstance(); last.settimeinmillis(lastdate); last.set(calendar.hour_of_day, last.getactualmaximum(calendar.hour_of_day)); last.set(calendar.minute, last.getactualmaximum(calendar.minute)); last.set(calendar.second, last.getactualmaximum(calendar.second)); last.set(calendar.millisecond, last.getactualmaximum(calendar.millisecond)); calendar todaystart = calendar.getinstance(); todaystart.set(calendar.hour_of_day, todaystart.getactualminimum(calendar.hour_of_day)); todaystart.set(calendar.minute, todaystart.getactualminimum(calendar.minute)); todaystart.set(calendar.second, todaystart.getactualminimum(calendar.second)); todaystart.set(calendar.millisecond, todaystart.getactualminimum(calendar.millisecond)); // if lastly recorded date before absolute minimum of today if ( last.before(todaystart) ) { todaycount = 0; } calendar thisweekstart = calendar.getinstance(); thisweekstart.set(calendar.hour_of_day, thisweekstart.getactualminimum(calendar.hour_of_day)); thisweekstart.set(calendar.minute, thisweekstart.getactualminimum(calendar.minute)); thisweekstart.set(calendar.second, thisweekstart.getactualminimum(calendar.second)); thisweekstart.set(calendar.day_of_week, thisweekstart.getfirstdayofweek()); thisweekstart.set(calendar.millisecond, thisweekstart.getactualminimum(calendar.millisecond)); // if lastly date before absolute minimum of week clear // week (and today, on safe side) if ( last.before(thisweekstart) ) { todaycount = 0; weekcount = 0; } calendar thismonthstart = calendar.getinstance(); thismonthstart.set(calendar.hour_of_day, thismonthstart.getactualminimum(calendar.hour_of_day)); thismonthstart.set(calendar.minute, thismonthstart.getactualminimum(calendar.minute)); thismonthstart.set(calendar.second, thismonthstart.getactualminimum(calendar.second)); thismonthstart.set(calendar.day_of_month, thismonthstart.getactualminimum(calendar.month)); thismonthstart.set(calendar.millisecond, thismonthstart.getactualminimum(calendar.millisecond)); // if lastly date before absolute minimum of month clear month... if ( !last.after(thismonthstart) ) { todaycount = 0; weekcount = 0; monthcount = 0; }
other readability challenges of using variable called "today" , setting manner of things aren't "today", you're not handling time.
if it's 3:20, , happened @ 5:00pm on jan 31st, want still count happening in january? should max out time related fields end of day well.
for week thing, can real mess if executes in locale sunday considered first day of week. may want consider using system's first day of week, rather sunday.
also worth noting depends explicitly on utilize of calendar.add()
work properly. cal.set(calendar.month, cal.get(calendar.month) -1);
not same thing , broken.
java android date calendar
No comments:
Post a Comment