67,513
社区成员
发帖
与我相关
我的任务
分享
package com.space.model;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Count {
private int totalCount;
private int todayCount;
private Date date = new Date();
private static Count instance = null;
public static Count getInstance() {
if (instance == null) {
instance = new Count();
}
return instance;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTodayCount() {
return todayCount;
}
public void setTodayCount(int todayCount) {
this.todayCount = todayCount;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public synchronized void addCount(Date date) {// 比较日期增加计数
if (new SimpleDateFormat("yyyy-MM-dd").format(this.getDate()).equals(
new SimpleDateFormat("yyyy-MM-dd").format(date))){
this.setTodayCount(this.getTodayCount() + 1);
}else{
this.setTodayCount(1);
}
this.setTotalCount(this.getTotalCount() + 1);
System.out.println("today->"+this.getTodayCount());
System.out.println("total->"+this.getTotalCount());
}
}
HttpSession session = request.getSession();
ServletContext context = request.getServletContext();
Count count = null;
count = (Count) context.getAttribute("count");
if (null == count) {
count = Count.getInstance();
}
if(session.isNew())
count.addCount(new Date());
context.setAttribute("count", count);