java - 我如何使用java确定某个事件是本周、上周还是哪周发生?

标签 java calendar

我正在开发一个简单的销售点系统,并试图生成尽可能多的销售和费用数据统计数据。我想要一种方法来显示本周的销售额,但是,尽管我浏览了 api 并检查了一些教程,但我确实找不到一种方法来做到这一点。最后,我决定将交易发生的 DAY_OF_WEEK_IN_MONTH 与今天的 DAY_OF_WEEK_IN_MONTH 进行比较。也就是说,如果它们相同,则交易一定是在与今天同一周(即本周)发生的。起初,它似乎有效,但现在我有了新的想法。如果有人向我指出正确的方法,我将不胜感激。

public void getSalesTotalForWeek() throws SQLException {
    //getDatesCount() populates a collection(listDates) with dates in which
    //transactions took place
    getDatesCount();
    //sets cal with today's date
    Calendar cal = new GregorianCalendar();
    for (int i = 0; i < listDates.size(); i++) {
        //set c with the date of when the transaction took place
        Calendar c = new GregorianCalendar(Integer.parseInt(listDates.get(i).substring(0, 4)),
        Integer.parseInt(listDates.get(i).substring(5, 7)), Integer.parseInt(listDates.get(i).substring(8, 10)));
        //This is like saying if the day of week in month when the transaction took place
        //is the same as that of today, then the transaction
        //must have taken place in the same week as today
        if(cal.get(Calendar.DAY_OF_WEEK_IN_MONTH) == c.get(Calendar.DAY_OF_WEEK_IN_MONTH)){
            rst = stmt.executeQuery("SELECT * FROM transaction_history where Time like '"+   listDates.get(i) + "%'");
            while (rst.next()) {
                weekSalesTotal += rst.getInt(2);
            }
        }
    }
}

最佳答案

您的代码中有一些需要修复的地方:

public void getSalesTotalForWeek()

名为 get*() 的方法并没有真正返回任何东西(违反直觉)。看起来像weekSalesTotal是一个字段,请记住线程安全。

<小时/>
Calendar cal = new GregorianCalendar();

如果有一天有人问你上周的销售额怎么办?还是三月的第一周?考虑将开始日期和结束日期作为参数传递。另外您确定要使用服务器默认时区吗?

<小时/>
Calendar c = new GregorianCalendar(Integer.parseInt(listDates.get(i).substring(0, 4)),
Integer.parseInt(listDates.get(i).substring(5, 7)), Integer.parseInt(listDates.get(i).substring(8, 10)));

listDatesList<String> ?考虑Date相反,上面的代码看起来很糟糕。

<小时/>
cal.get(Calendar.DAY_OF_WEEK_IN_MONTH) == c.get(Calendar.DAY_OF_WEEK_IN_MONTH)

我不太明白这段代码。我正在阅读 DAY_OF_WEEK_IN_MONTH 的 JavaDoc我还是没明白...

<小时/>
"SELECT * FROM transaction_history where Time like '"+   listDates.get(i) + "%'"

如果您只对第二列感兴趣(我们称之为 value ),请不要选择所有列。也不要连接 SQL,SQL 注入(inject)已经在路上了。最后使用like日期运算符?您使用正确的DATE吗?输入数据库?

<小时/>

weekSalesTotal一个字段?请记住同步。并避免在循环中运行 SQL。

TL;DR

我相信你的整个方法可以被更强大、更快、更简单的东西取代:

public int getSalesTotalWithin(Date start, Date end) {
  rst = stmt.executeQuery(
    "SELECT SUM(value) FROM transaction_history where Time BETWEEN ? AND ?");
  return rst.getInt(1);
}

这只是伪代码,它缺少 JDBC 样板和 ?代换。但你会明白的。现在您可以使用任何日期范围调用它,例如:

Calendar weekAgo = new GregorianCalendar();
weekAgo.add(Calendar.DATE, -7);
getSalesTotalWithin(weekAgo.getTime(), new Date());

如果您想要自上周一以来的统计数据:

Calendar lastMonday = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
lastMonday.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
lastMonday.set(Calendar.HOUR_OF_DAY, 0);
//...zero other fields
getSalesTotalWithin(lastMonday.getTime(), new Date());

关于java - 我如何使用java确定某个事件是本周、上周还是哪周发生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13324665/

相关文章:

java - java计算月份和年份的总天数

java - 在 Java 中检索正确 cookie 的问题

java - 具有不同对象的列表

Java:检查循环中的当前项目是否是正在循环的堆栈中的最后一个项目

android - 如何循环遍历gridview android

javascript - 添加 onClick 事件到 zabuto 日历

java - Spring - 停止 bean 初始化

java.net.SocketException : Connection reset by peer: socket write error

android - 在 TimePicker 中设置时间并将其与 calendar.hour 和 minute 进行比较?

java - 获取到特定时间点的时间