我有一个显示用户活动的JSF表。
<p:lineChart id="logins" value="#{StatisticsController.weekActivity}" legendPosition="e"
title="Weekly Logins" seriesColors="4D94FF, 1975FF, 005CE6, 0047B2" minY="0" maxY="200"/>
public class Statistics implements Serializable {
private CartesianChartModel weeksActivity;
public Statistics() {
createweeksActivity();
}
public CartesianChartModel getweekActivity() {
return weeksActivity;
}
private void createweeksActivity() {
weeksActivity = new CartesianChartModel();
ChartSeries boys = new ChartSeries();
boys.setLabel("Active Accounts");
boys.set("Monday", 120);
boys.set("Tuesday", 100);
boys.set("Wednesday", 44);
boys.set("Thursday", 150);
boys.set("Friday", 120);
boys.set("Saturday", 82);
boys.set("Sunday", 115);
ChartSeries girls = new ChartSeries();
girls.setLabel("Blocked Accounts");
girls.set("Monday", 52);
girls.set("Tuesday", 60);
girls.set("Wednesday", 110);
girls.set("Thursday", 135);
girls.set("Friday", 120);
girls.set("Saturday", 140);
girls.set("Saturday", 130);
girls.set("Sunday", 140);
weeksActivity.addSeries(boys);
weeksActivity.addSeries(girls);
}
}
我想按时顺序显示当前日期。例如:
现在我收到了这个订单:Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
如果今天是星期三,我想在打开网页时收到此订单:
Thursday, Friday, Saturday, Sunday, Monday, Tuesday, Wednesday
我如何解决这个问题?
EDIT:
我用这种方式更新了代码:
private void createweeksActivity()
{
weeksActivity = new CartesianChartModel();
ChartSeries activeAccounts = new ChartSeries();
activeAccounts.setLabel("Active Accounts");
ChartSeries blockedAccounts = new ChartSeries();
blockedAccounts.setLabel("Blocked Accounts");
GregorianCalendar calendar = new GregorianCalendar();
DateFormat df = new SimpleDateFormat("EEEE");
for (int i = 0; i < 7; i++)
{
calendar.add(Calendar.DATE, 1);
System.out.println(df.format(calendar.getTime()));
activeAccounts.set(df.format(calendar.getTime()), getNumberOfLogins(df.format(calendar.getTime())));
blockedAccounts.set(df.format(calendar.getTime()), getNumberOfLogins(df.format(calendar.getTime())));
}
weeksActivity.addSeries(activeAccounts);
weeksActivity.addSeries(blockedAccounts);
}
public int getNumberOfLogins(String day){
// Get here the number of logins using SQL command
// Example: SELECT * FROM HISTORY WHERE DAY = day;
return 100;
}
我想它会起作用。
答案 0 :(得分:5)
GregorianCalendar calendar = new GregorianCalendar();
DateFormat df = new SimpleDateFormat("EEEE");
for (int i = 0; i < 7; i++) {
calendar.add(Calendar.DATE, 1);
System.out.println(df.format(calendar.getTime()));
}
GregorianCalendar表示我们的格里高利历下的瞬间。它的无参数构造函数将其初始化为当前时间。
DateFormat用于格式化日期。您可以使用(Gregorian)Calendar
方法将getTime()
转换为日期。 “EEEE”是指一周中最长的一天。
(Gregorian)Calendar.add()
方法允许向日历的任何字段(年,月,日,小时等)添加值。由于你想要迭代一周中的几天,我从明天开始,每次迭代都加1天。
the javadoc中提供了所有内容。
答案 1 :(得分:1)
根据日历,将你的星期几周作为地图的星期几。
然后使用Calendar获取当前日期并使用Modular Arithmetic来更改排序。
类似的东西:
//This you can do it one time and then use it in your program
Map<Integer, String> daysOfWeek = new HashMap<Integer,String>();
daysOfWeek.put(Calendar.MONDAY, "Monday");
daysOfWeek.put(Calendar.TUESDAY, "Tuesday");
//so on and so forth.
//Now the code that will be running
//You get your current Day (Which will be the last one you want to see)
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
//Start with next value (modulus 7 since you don't want overflow)
int currDay = (weekday+1) % 7;
//Go over the seven Days
for(int i = 0; i < 7; i++) {
String currDay = daysOfWeek.get(currDay); //Get your Day String
//Here do whatever you need to do to set up the DAY in JSF
currDay = (currDay+1) % 7; //Moving currDay to the next Day
}