I am working on an Android app that will display a list of activities. Every activity (i.e. waling, running) has a property Date (i.e. 9 March 8:58 2017). I have two buttons on the screen - Daily and Weekly and I want to switch betweren the two and change the list accordingly. Now, for the Daily list, I don't have to change anything, since a new Activity is created for every day.
However, I am not sure how to go about the Weekly list. It will essentially calculate stats (adding up the statistics for the individual weeks).
For example, I have a list of dates for the last 50 days. How to distinguish an individual list of Dates that would represent an individual week so I can construct a list of Weeks? Basically, convert those 50 dates into their week equivalent (e.g. about 7 weeks)
This is a test list of Dates that I am trying to get working first:
HashMap<Integer,Date> dateHashMap = new HashMap<>();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
List<Date> dates = new ArrayList<>();
dates.add(sdf.parse("10/03/2017"));
dates.add(sdf.parse("9/03/2017"));
dates.add(sdf.parse("8/03/2017"));
dates.add(sdf.parse("7/03/2017"));
dates.add(sdf.parse("6/03/2017"));
dates.add(sdf.parse("23/02/2017"));
dates.add(sdf.parse("3/02/2017"));
dates.add(sdf.parse("2/02/2017"));
dates.add(sdf.parse("1/02/2017"));
for(Date d:dates){
dateHashMap.put(d.getDay(),d);
}
System.out.println(dateHashMap.toString());
An example UI design that I am trying to achieve:
Week?
You have not defined what you mean by week.
Time zone?
What time zone do want to use as the context for determining the date? Or do you want to keep your date-times in UTC like Stack Overflow does in tracking your activity for “today” vs “yesterday”?
Avoid legacy date-time classes
The troublesome old date classes including
Date
andCalendar
should be avoided whenever possible. They are now supplanted by the java.time classes.Convert your given
Date
objects toInstant
, a moment on the timeline in UTC.ISO 8601
I suggest using the standard week whenever possible.
Adjust your
Instant
into the desired time zone.Retrieve the standard week number.
Keep in mind that the year number to go with this week number is not the calendar year. We want the year of the week-based year. For example, in some years, December 30 and 31 can belong to the following year number of a week-based year.
You could track your records against a string composed of this
yearOfWeekBasedYear
andweekNumber
. Use standard format, yyyy-Www such as2017-W07
.ThreeTen-Extra
YearWeek
Instead I suggest you use meaningful objects rather than mere strings. Add the ThreeTen-Extra library to your project to gain the
YearWeek
class.This code replaces the
WeekFields
code we did above.As you already have Date property for each activity, then its quite simple actually
First just decide how you want your weeks
ex: I would just go from
Mon
toSun
as one weekSo here
Week nth
will have dates -1st to 5th March
and
Week nth+1
will have6th to 12th March
and so on.. and as far as i can understand you already have every activity (i.e. waling, running) with a property Date (i.e. 9 March 8:58 2017)So taking an example here (let me know if this isn't how you have your data) :
waling
-1 March 2017 8:58 to 9:58
,3 March 2017 6:20 to 6:50
,8 March 2017 12:00 to 13:00
running
-2 March 2017 6:10 to 8:00
,3 2017 March 7:00 to 8:00
,9 March 2017 5:50 to 7:00
Now data for
Week nth
you can calculate by adding up duration forwaling
activity for dates1st
and3rd March
aswaling
was present only on these dates onWeek nth
ofMarch 2017
and similarly forweek nth+1
and so onSame goes for
running
activity forweek nth
adding up for dates2nd March
,3rd March
and similarly forweek nth+1
and so on..Now you will have something like :
And on clicking of each activity you can show some more details..
Hope this helps :)
Edit :
Considering this is how you have your dates list
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
You can create a custom
List
just to identify the dates that falls in the same weekex (I just used what @Jameson suggested in his answer, you can always write this a lot better):
And you can use
getListOfWeeksFromListOfDates(dates);
to have a list withDates
andWeek
number, thisweek
number can serve as an identifier to compare the dates and then you can add the activities for dates with sameWeek
number..Hope you are getting what i am trying to convey here :)
Thanks to @shadygoneinsane and @ Basil Bourque for pointing me to the right direction I solved the problem the following way:
And the result:
Output:
Exactly what I needed! So now I can iterate through each week and sum up the statistics and thus formulate the "Weekly" view of the list