日历未验证仅年月和日

时间:2012-10-17 08:58:56

标签: java swing date calendar

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

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

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

2 个答案:

答案 0 :(得分:5)

月常数10适用于11月。 NOVEMBER = 10所以你将月份定为11月。

你应该使用

expiredate.set(2012, Calendar.OCTOBER, 10);

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

答案 1 :(得分:1)

问题是,Calendar的月份值是从0开始的。这意味着,您的到期日期是11月10日。

10月10日将是:

expiredate.set(2012, 9 , 10);

希望这有帮助

相关问题