我正在写一个比较代码的日期,以检查是否是新月,并将我的余额重置为655,000,从每个月的第一天开始生效。代码应该检查当前日期是否等于第1天下个月,如果currentmonth不等于下个月,并从 655000 中减去我的输入,假设它是该月的第一笔交易。在此之后,我应该立即决定输入新的交易,第一个成本是100美元,这意味着我现在 654,900 ,我想在第二次交易后存入余额( 654900(第二次交易的总成本)< / strong>)并且应该跟随所有后续交易的套件,直到当前日= 1 并且当前月== nextmonth然后整个过程将重新开始,新的第一天的第一个交易具有其从 655000 中减去总费用,然后所有其他交易将在套件
之后在我的数据库中这是我期待的输出。我很抱歉早先没有简明扼要的解释
**CustomerID||InvoiceID||BalanceDate ||ChequeSum||CreditSum||CashSum||ReversalSum||Balance**
201743112026 10286202 2017-04-18 13:12:01 100.0 0.0 0.0 0.0 654900 (655000-100)
201743112021 10476202 2017-04-18 13:12:09 609.0 0.0 0.0 0.0 654291 (654900-100)
private void dayCount() {
Date theDate = new Date();
Calendar myCal = new GregorianCalendar();
myCal.setTime(theDate);
int currentDay = myCal.get(Calendar.DAY_OF_MONTH);
int currentMonth = myCal.get(Calendar.MONTH) + 1;
int currentYear = myCal.get(Calendar.YEAR);
int nextday = currentDay + 1;
int nextMonth = currentMonth + 1;
int tempMonth = currentMonth;
System.out.println("Day: " + currentDay);
System.out.println("Month: " + currentMonth);
System.out.println("Year: " + currentYear);
System.out.println("nextday: " + nextday);
System.out.println("nextMonth: " + nextMonth);
System.out.println("tempMonth: " + tempMonth);
if (currentDay != 1 && currentMonth == tempMonth && currentMonth != nextMonth) {
if (counts == 0) {// count returns zero if no tranction has occured so deduction starts frim 655000
if (countz == 0) {// countz returns 0 if no cash,credit or cheque transaction has been computed yet and 1 if vice versa
System.out.println("counts value is " + counts);
FirstEntry();
}
} else if (counts == 1) {
if (countz != 0) {
System.out.println("counts value is " + counts);
LatterEntry();
}
}
} else {
FirstEntry();
}
}
/* currentRBalance returns cuurent balance in database */
private void FirstEntry() {
transT = transType.getSelectedItem().toString();
if (transT.equals("Cash")) {
try { // actual crud functions can be undertaken
stmt = DBConnection.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
con = DBConnection.getConnection();
String sql;
sql = "insert into reducingbalances (CustomerID, InvoiceID, BalanceDate, ChequeSum, CreditSum, CashSum, ReversalSum, Balance) values (?,?,?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, code);
ps.setString(2, cusInv.getSelectedItem().toString());
ps.setString(3, ((JTextField) invDate.getDateEditor().getUiComponent()).getText());
ps.setString(4, "" + 0.00);
ps.setString(5, "" + 0.00);
ps.setString(6, ovrallT.getText());
ps.setString(7, "" + 0.00);
ps.setString(8, "" + (655000.0 - Double.parseDouble(invtotal.getText())));
ps.execute();
JOptionPane.showMessageDialog(this, "Record saved successfully");
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Unable to insert record 1 " + e.getMessage());
}
} else if (transT.equals("Cheque")) {
try { // actual crud functions can be undertaken
stmt = DBConnection.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
con = DBConnection.getConnection();
String sql;
sql = "insert into reducingbalances (CustomerID, InvoiceID, BalanceDate, ChequeSum, CreditSum, CashSum, ReversalSum, Balance) values (?,?,?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, code);
ps.setString(2, cusInv.getSelectedItem().toString());
ps.setString(3, ((JTextField) invDate.getDateEditor().getUiComponent()).getText());
ps.setString(4, "" + ovrallT.getText());
ps.setString(5, "" + 0.00);
ps.setString(6, "" + 0.00);
ps.setString(7, "" + 0.00);
ps.setString(8, "" + (655000.0 - Double.parseDouble(invtotal.getText())));
ps.execute();
JOptionPane.showMessageDialog(this, "Record saved successfully");
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Unable to insert record 2 " + e.getMessage());
}
} else if (transT.equals("Credit")) {
try { // actual crud functions can be undertaken
stmt = DBConnection.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
con = DBConnection.getConnection();
String sql;
sql = "insert into reducingbalances (CustomerID, InvoiceID, BalanceDate, ChequeSum, CreditSum, CashSum, ReversalSum, Balance) values (?,?,?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, code);
ps.setString(2, cusInv.getSelectedItem().toString());
ps.setString(3, ((JTextField) invDate.getDateEditor().getUiComponent()).getText());
ps.setString(4, "" + 0.00);
ps.setString(5, "" + ovrallT.getText());
ps.setString(6, "" + 0.00);
ps.setString(7, "" + 0.00);
ps.setString(8, "" + (655000.0 - Double.parseDouble(invtotal.getText())));
ps.execute();
countz++;
JOptionPane.showMessageDialog(this, "Record saved successfully");
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Unable to insert record 3 " + e.getMessage());
}
}
countz++;
}
private void LatterEntry() {
transT = transType.getSelectedItem().toString();
if (transT.equals("Cash")) {
try { // actual crud functions can be undertaken
stmt = DBConnection.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
con = DBConnection.getConnection();
String sql;
sql = "insert into reducingbalances (CustomerID, InvoiceID, BalanceDate, ChequeSum, CreditSum, CashSum, ReversalSum, Balance) values (?,?,?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, code);
ps.setString(2, cusInv.getSelectedItem().toString());
ps.setString(3, ((JTextField) invDate.getDateEditor().getUiComponent()).getText());
ps.setString(4, "" + 0.00);
ps.setString(5, "" + 0.00);
ps.setString(6, ovrallT.getText());
ps.setString(7, "" + 0.00);
ps.setString(8, "" + (myCurrentRBalance - Double.parseDouble(invtotal.getText())));
ps.execute();
InvDate = ((JTextField) invDate.getDateEditor().getUiComponent()).getText();
System.out.println(invDate + "------");
JOptionPane.showMessageDialog(this, "Record saved successfully");
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Unable to insert record 4" + e.getMessage());
}
} else if (transT.equals("Credit")) {
try { // actual crud functions can be undertaken
stmt = DBConnection.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
con = DBConnection.getConnection();
String sql;
sql = "insert into reducingbalances (CustomerID, InvoiceID, BalanceDate, ChequeSum, CreditSum, CashSum, ReversalSum, Balance) values (?,?,?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, code);
ps.setString(2, cusInv.getSelectedItem().toString());
ps.setString(3, ((JTextField) invDate.getDateEditor().getUiComponent()).getText());
ps.setString(4, ovrallT.getText());
ps.setString(5, "" + 0.00);
ps.setString(6, "" + 0.00);
ps.setString(7, "" + 0.00);
ps.setString(8, "" + (myCurrentRBalance - Double.parseDouble(invtotal.getText())));
ps.execute();
InvDate = ((JTextField) invDate.getDateEditor().getUiComponent()).getText();
System.out.println(invDate + "------");
JOptionPane.showMessageDialog(this, "Record saved successfully");
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Unable to insert record 5 " + e.getMessage());
}
} else if (transT.equals("Credit")) {
try { // actual crud functions can be undertaken
stmt = DBConnection.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
con = DBConnection.getConnection();
String sql;
sql = "insert into reducingbalances (CustomerID, InvoiceID, BalanceDate, ChequeSum, CreditSum, CashSum, ReversalSum, Balance) values (?,?,?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, code);
ps.setString(2, cusInv.getSelectedItem().toString());
ps.setString(3, ((JTextField) invDate.getDateEditor().getUiComponent()).getText());
ps.setString(4, "" + 0.00);
ps.setString(5, ovrallT.getText());
ps.setString(6, "" + 0.00);
ps.setString(7, "" + 0.00);
ps.setString(8, "" + (myCurrentRBalance - Double.parseDouble(invtotal.getText())));
ps.execute();
InvDate = ((JTextField) invDate.getDateEditor().getUiComponent()).getText();
System.out.println(invDate + "------");
JOptionPane.showMessageDialog(this, "Record saved successfully");
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Unable to insert record 6 " + e.getMessage());
}
}
}
答案 0 :(得分:1)
我不太确定你想要获得什么,但我想我可以把你想要的东西给你,我相信你会把它们正确地放在一起以达到你的目的。由于您使用的是Java 8,因此请从一开始就将您的日期时间作为java.time
类实例,或转换为您获得的java.util.Date
。新类的使用起来非常好,所以既然你需要转换(转换为Calendar
),你也可以转换为更好的类。以下是转换为ZonedDateTime
的方法:
ZonedDateTime theDateTime = theDate.toInstant().atZone(ZoneId.systemDefault());
Month theMonth = theDateTime.getMonth();
boolean isFirstDayOfMonth = theDateTime.getDayOfMonth() == 1;
该代码段还显示了如何从日期中提取月份和日期,因为这些是您处理过程中所需的。 Month
是一个枚举,因此您可以使用==
安全地将当前交易的月份与上一个交易的月份进行比较。