我创建了以下方法,该方法返回一个HashMap<Date, List<Date>>
,其中键是Date
对象,而值是List
个对象的Date
。该方法接受List
中的timeStamps
,并按天分组。然后,它返回上述HashMap
构造中的那些分组的时间戳。
public class GroupDatesByDay {
HashMap<Date, List<Date>> groupedUserLogins = new HashMap<Date, List<Date>>();
Calendar cal = Calendar.getInstance();
public HashMap<Date, List<Date>> parseTimeStamps(List<Date> timeStamps) {
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS", Locale.US);
List<Date> timeStamps = new ArrayList<Date>();
for (Date ts : timeStamps) {
cal.setTime(ts);
cal.set(cal.HOUR_OF_DAY, 0);
cal.set(cal.MINUTE, 0);
cal.set(cal.SECOND, 0);
cal.set(cal.MILLISECOND, 0);
if (!groupedUserLogins.containsKey(cal.getTime())) {
groupedUserLogins.put(cal.getTime(), new ArrayList<Date>());
}
groupedUserLogins.get(cal.getTime()).add(ts);
}
keySet = groupedUserLogins.keySet();
keyList.addAll(keySet);
return groupedUserLogins;
}
}
HashMap中的数据应如下所示:
Key List<Date>
2018-07-11
2018-07-11 08:14:08.540000
2018-07-11 10:46:23.575000
2018-07-12
2018-07-12 12:51:48.928000
2018-07-12 13:09:00.701000
2018-07-12 16:04:45.890000
2018-07-13
2018-07-13 14:14:17.461000
在我的XHTML中,我想每天在dataTable和RowExpansion中显示此数据,以查看各个时间戳。我已经编写了以下XHTML。如您所见,userLogins
是我的Java方法返回的内容。我正在此dataTable
中对其进行迭代,但是我不知道如何以上述方式在其中显示值。
<p:dataTable var="userLogin" value="#{userEdit.userLogins}"class="DataTable">
<p:column headerText=" Recent Logins">
<h:outputLabel value="#{userLogin}">
</h:outputLabel>
</p:column>
</p:dataTable>
答案 0 :(得分:1)
这是我终于解决它的方法。根据Melloware的建议,我必须加入一个列表。
我的实现略有变化:
private Hashtable<Date, List<Date>> groupedUserLogins = new Hashtable<Date, List<Date>>();
private Calendar cal = Calendar.getInstance();
private Set<Date> keySet = new TreeSet<Date>();
private List<Date> keyList = new ArrayList<Date>();
...
public Hashtable<Date, List<Date>> parseUserLogins(List<UserLogin> userLogins) {
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS", Locale.US);
List<Date> timeStamps = new ArrayList<Date>();
for (UserLogin u : userLogins) {
timeStamps.add(u.getTimeStamp());
}
for (Date ts : timeStamps) {
cal.setTime(ts);
cal.set(cal.HOUR_OF_DAY, 0);
cal.set(cal.MINUTE, 0);
cal.set(cal.SECOND, 0);
cal.set(cal.MILLISECOND, 0);
dateFormat.format(ts);
if (!groupedUserLogins.containsKey(cal.getTime())) {
groupedUserLogins.put(cal.getTime(), new ArrayList<Date>());
}
groupedUserLogins.get(cal.getTime()).add(ts);
}
keySet = groupedUserLogins.keySet();
keyList.addAll(keySet);
return groupedUserLogins;
}
我的XHTML:
<p:dataTable var="loginDayDate" value="#{userEdit.keyList}"
sortOrder="descending" sortBy="#{loginDayDate}" class="DataTable">
<p:column width="15">
<p:rowToggler />
</p:column>
<p:column headerText=" Recent Logins" width="110"
sortBy="#{loginDayDate}">
<h:outputLabel value="#{loginDayDate}">
<f:convertDateTime pattern="#{Constants.DATE_FORMAT}" />
</h:outputLabel>
</p:column>
<p:column headerText=" # of Logins" style="text-align : left">
#{fn:length(userEdit.groupedUserLogins[loginDayDate])}
</p:column>
<p:rowExpansion>
<p:dataTable var="specificDate"
value="#{userEdit.groupedUserLogins[loginDayDate]}"
class="DataTable">
<p:column headerText="Time" style="text-align: left">
<h:outputLabel value="#{specificDate}">
<f:convertDateTime pattern="#{Constants.DATETIME_FORMAT}" />
</h:outputLabel>
</p:column>
</p:dataTable>
</p:rowExpansion>
</p:dataTable>
外观: