私はシンプルなPOSシステムに取り組んでおり、売上と経費のデータについてできるだけ多くの統計を生成しようとしていました。今週の売り上げを表示する方法が欲しかったのですが、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);
}
}
}
}