如何在Java中将日期列表分组到对应的星期? [英] How to group list of Dates to corresponding week in Java?
问题描述
我正在使用一个Android应用程序,它将显示活动列表.每个活动(即哭号,跑步)都具有日期属性(即2017年3月9日8:58).我在屏幕上有两个按钮-每日和每周,我想在两个按钮之间进行切换,并相应地更改列表.现在,对于每日"列表,我无需更改任何内容,因为每天都会创建一个新的活动".
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).
例如,我有最近50天的日期列表.如何区分代表单个星期的单个日期列表,以便构建一个星期列表?基本上,将这50个日期转换成相当于一周的时间(例如约7周)
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());
我试图实现的示例UI设计:
An example UI design that I am trying to achieve:
推荐答案
由于每个活动都有Date属性,因此实际上非常简单
As you already have Date property for each activity, then its quite simple actually
首先,请确定您希望自己的星期如何
例如:我只需要一周从Mon
转到Sun
First just decide how you want your weeks
ex: I would just go from Mon
to Sun
as one week
因此,这里Week nth
将具有日期-1st to 5th March
和Week nth+1
将具有6th to 12th March
,依此类推.
据我了解,您已经进行了具有日期属性(即2017年3月9日8:58)的所有活动(即哭号,跑步)
So here Week nth
will have dates - 1st to 5th March
and Week nth+1
will have 6th 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)
所以在这里举个例子(让我知道这是否不是您的数据):
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
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
Week nth
的现在数据,您可以通过将日期1st
和3rd March
的waling
活动持续时间相加来计算,因为waling
仅出现在March 2017
的Week nth
上的这些日期上,并且类似地对于week nth+1
等
对于week nth
的running
活动也是如此,对日期2nd March
,3rd March
进行总计,对week nth+1
进行类似处理,依此类推.
Now data for Week nth
you can calculate by adding up duration for waling
activity for dates 1st
and 3rd March
as waling
was present only on these dates on Week nth
of March 2017
and similarly for week nth+1
and so on
Same goes for running
activity for week nth
adding up for dates 2nd March
, 3rd March
and similarly for week nth+1
and so on..
现在您将拥有类似:
Now you will have something like :
第n周:
- 哭泣-1小时30分钟
- 跑步-2小时50分钟
- 哭泣-1小时
- 运行-1小时10分钟
在单击每个活动时,您可以显示更多详细信息..
希望这可以帮助 :)
考虑到这是日期列表的方式 - Wailing - 1 hr
- Running - 1 hr and 10 min
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
第n + 1周:
Week nth+1 :
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
List<Date> dates = new ArrayList<>();
try {
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"));
} catch (ParseException e) {
e.printStackTrace();
}
您可以创建一个自定义List
只是为了确定同一周
ex的日期(我只是使用@Jameson在他的回答中建议的内容,您总是可以写得更好):>
You can create a custom List
just to identify the dates that falls in the same week
ex (I just used what @Jameson suggested in his answer, you can always write this a lot better):
public List<WeekDay> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
List<WeekDay> listOfWeeks = new ArrayList<>();
WeekDay weekDay;
for (Date date : listOfDates) {
weekDay = new WeekDay(date, new SimpleDateFormat("w").format(date));
listOfWeeks.add(weekDay);
}
return listOfWeeks;
}
public class WeekDay {
Date date;
String weekIdentifier;
public WeekDay(Date Date, String WeekIdentifier) {
this.date = Date;
this.weekIdentifier = WeekIdentifier;
}
public Date getDate() {
return date;
}
public String getWeekIdentifier() {
return weekIdentifier;
}
}
然后您可以使用getListOfWeeksFromListOfDates(dates);
列出具有Dates
和Week
编号的列表,该week
编号可以用作比较日期的标识符,然后您可以添加具有相同Week
号..
希望您能得到我正在尝试传达的内容:)
And you can use getListOfWeeksFromListOfDates(dates);
to have a list with Dates
and Week
number, this week
number can serve as an identifier to compare the dates and then you can add the activities for dates with same Week
number..
Hope you are getting what i am trying to convey here :)
这篇关于如何在Java中将日期列表分组到对应的星期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!