Java 如何在周末重置Arraylist?
我有一个arraylist:Java 如何在周末重置Arraylist?,java,android,sqlite,arraylist,Java,Android,Sqlite,Arraylist,我有一个arraylist: ArrayList<String> wholeWeekArrayList = new ArrayList<>(); 然后我有了SumofWeek,其中wholeWeekArrayListString中的所有数据被添加到一起。 (我将wholeWeekArrayListString转换为double以在文本视图中查看它) 我希望这个SumOfWeek在周末重置为零,但SQLite中的数据必须不断添加(对于SumOfMonth视图) 因此,到
ArrayList<String> wholeWeekArrayList = new ArrayList<>();
SumofWeek
,其中wholeWeekArrayListString
中的所有数据被添加到一起。
(我将wholeWeekArrayListString
转换为double以在文本视图中查看它)SumOfWeek
在周末重置为零,但SQLite中的数据必须不断添加(对于SumOfMonth
视图)
因此,到周日,数据可能是50.00美元(例如),从周一开始计算。数据必须重置为0.0
,并一直累加到周日。
这必须每周发生一次Monday =1;
Tuesday = 2;
Wednesday =3;
// ...
迭代整个星期,但我不能得到(i),这是一周中每一天的数据,然后重置它,它是
int Sunday =7;
我有总数,但不知道在周末用什么方法重置数据?在不更改DB调用的情况下,您必须存储最新重置的时间戳(下面的示例中为resetTimestamp)。如果当前时间在不同的一周内,代码将触发sumofWeek的重置。在那之后,这是你正常的周末逻辑 线程安全性:根据应用程序的具体情况添加
private ArrayList<Long> sumofWeek = new ArrayList<>();
private Long resetTimestamp;
public void populateWeek() {
if(resetTimestamp == null || isDifferentWeek(resetTimestamp)) {
sumofWeek = new ArrayList<Long>();
for(int i = 0; i < 7; i++){
sumofWeek.add(0l);
}
System.out.println(sumofWeek);
resetTimestamp = java.lang.System.currentTimeMillis();
}
// populate sumofWeek here
}
private boolean isDifferentWeek(long resetTimestamp) {
Calendar cl1 = new GregorianCalendar();
cl1.setTimeInMillis(java.lang.System.currentTimeMillis());
Calendar cl2 = new GregorianCalendar();
cl2.setTimeInMillis(resetTimestamp);
return cl1.get(Calendar.WEEK_OF_YEAR) != cl2.get(Calendar.WEEK_OF_YEAR);
}
private ArrayList sumofWeek=new ArrayList();
私有长重置时间戳;
公共无效人口周(){
if(resetTimestamp==null | | isDifferentiweek(resetTimestamp)){
sumofWeek=newarraylist();
对于(int i=0;i<7;i++){
周加总油量(0升);
}
系统输出打印LN(sumofWeek);
resetTimestamp=java.lang.System.currentTimeMillis();
}
//这里是sumofWeek
}
专用布尔值IsDifferentiweek(长重置时间戳){
日历cl1=新的格里高利安日历();
cl1.setTimeInMillis(java.lang.System.currentTimeMillis());
日历cl2=新的格里高利安日历();
cl2.setTimeInMillis(重置时间戳);
返回cl1.get(日历年中的星期)!=cl2.get(日历年中的星期);
}
private ArrayList<Long> sumofWeek = new ArrayList<>();
private Long resetTimestamp;
public void populateWeek() {
if(resetTimestamp == null || isDifferentWeek(resetTimestamp)) {
sumofWeek = new ArrayList<Long>();
for(int i = 0; i < 7; i++){
sumofWeek.add(0l);
}
System.out.println(sumofWeek);
resetTimestamp = java.lang.System.currentTimeMillis();
}
// populate sumofWeek here
}
private boolean isDifferentWeek(long resetTimestamp) {
Calendar cl1 = new GregorianCalendar();
cl1.setTimeInMillis(java.lang.System.currentTimeMillis());
Calendar cl2 = new GregorianCalendar();
cl2.setTimeInMillis(resetTimestamp);
return cl1.get(Calendar.WEEK_OF_YEAR) != cl2.get(Calendar.WEEK_OF_YEAR);
}