日历未验证仅按月和日的年份

Err*_*ion 1 java swing calendar date

我有一个操作按钮,用于在登录期间检查应用程序的过期日期:

public void actionPerformed(ActionEvent ae) {
  Calendar expiredate = Calendar.getInstance();
  expiredate.set(2012, 10, 10);
  if (ae.getSource() == button) {
    char[] temp_pwd = t_pass.getPassword();
    String pwd = null;
    pwd = String.copyValueOf(temp_pwd);

    if (db.checkLogin(t_name.getText(), pwd)) {
      try {
        if (Calendar.getInstance().after(expiredate)) {
          JOptionPane.showMessageDialog(null, "License has Expired\n Please Re-new the License from the Provider", "Re-new License", JOptionPane.ERROR_MESSAGE);
          t_name.setText("");
          t_pass.setText("");
          t_name.requestFocus();
          return;
        }
        JOptionPane.showMessageDialog(null, "You have logged in successfully. Click OK to Continue", "Success",
        JOptionPane.INFORMATION_MESSAGE);
        MainFrame page = new MainFrame();
        page.setVisible(true);
        setVisible(false);
      } catch(Exception ai){
        JOptionPane.showMessageDialog(null, ai, "Exception",
        JOptionPane.INFORMATION_MESSAGE);
      }
    } else {
      JOptionPane.showMessageDialog(null, "Login failed!\nWrong Username or password", "Failed!!",
      JOptionPane.ERROR_MESSAGE);
      t_name.setText("");
      t_pass.setText("");
      t_name.requestFocus();
      return;
    }
  }//if
}//method
Run Code Online (Sandbox Code Playgroud)

问题是系统日期是2012年10月17日.显然登录仍然发生.但是当我将2012年到2011年更改为过期日期时,许可证验证就会发生.该系统忽视2012年10月10日的日期可能是什么问题?

Ami*_*nde 5

月份常数10是11月.NOVEMBER = 10所以你把月份定为11月.

你应该用

expiredate.set(2012, Calendar.OCTOBER, 10);
Run Code Online (Sandbox Code Playgroud)

始终使用Calendar中提供的月常量来进行此类用法,因为月份从0java 开始.所以它避免了混乱.