将ArrayList中的项转换为不同的类型

时间:2014-04-14 02:15:36

标签: java arraylist simpledateformat

我的程序中有一个用户登录的部分可以访问应用程序,当用户登录时我有一个时间戳记并被存储到ArrayList中。当用户注销时,我会在注销时获得另一个时间戳。我一直试图相互减去2次,但它不允许我这样做。它们存储在String ArrayList中,我知道你不能减去String,所以我尝试过转换为double,int,date等......这些都不适用于我。我的问题是我想要两次之间的区别。我也知道在SO上还有其他与此相似的问题,但没有针对这个确切的问题,这就是我发布它的原因。我不是要发布重复。

代码:

 loginButton.addActionListener(
        new ActionListener()
        {
           //method for events that will be performed when 'loginButton' is pressed
           public void actionPerformed(ActionEvent e)
           {                   
              try
              {                   
                 //gets texts from specified text fields and assigns to instance variable
                 String userNameFromLogin = userNameTextBox.getText().trim();
                 String password = passwordTextBox.getText().trim();

                 //instance variable and object that holds currrent time when logged in                    
                 SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm");
                 Date date = new Date();
                 String loginTime = dateFormat.format(date);

                 //sql statement that check if username and password exist
                 String sql5 = "SELECT User_name,Password FROM Employee_Table WHERE User_name = '" + userNameFromLogin + "' and Password = '" + password + "'";

                 //execute query, assigning all records in db to 'rs5'
                 rs5 = st.executeQuery(sql5);

                 //instance variables
                 int count = 0;

                 //loops until reaches end up 'rs5'
                 while(rs5.next())
                 {
                    count++;
                 }

                 //statement and actions if 'userName' and 'password' match
                 if(count == 1)
                 { 
                    //sets 'welcomeFrame' to true                    
                    welcomeFrame.setVisible(true);

                    //sets 'loginFrame' to false
                    loginFrame.setVisible(false);

                    //sets text fields to blank
                    userNameTextBox.setText("");
                    passwordTextBox.setText("");

                    //adds 'userNameFromLogin' to array
                    loginArray.add(userNameFromLogin);

                    //adds 'loginTime' to array
                    loginArray.add(loginTime);

                    //sets label to current username logged in                       
                    userNameLabel.setText("logged in as: " + userNameFromLogin);   
                 }

                 //statement and actions if 'userName' and 'password' do not match 
                 else
                 {
                    JOptionPane.showMessageDialog(null, "Username or password incorrect!");
                    userNameTextBox.setText("");
                    passwordTextBox.setText("");
                 }   
              }

              catch(Exception ex)
              {

              }               
           }
        });
}

logoutHomeButton.addActionListener(
        new ActionListener()
        {
           //method for events that will be performed when 'employeeFormButton' is pressed
           public void actionPerformed(ActionEvent e)
           {
              try
              {
                 //instance variable and object that holds currrent time when logged in                    
                 SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm");
                 Date date = new Date();
                 String logoutTime = dateFormat.format(date);

                 //sets 'loginFrame' to visible
                 loginFrame.setVisible(true);

                 //sets 'welcomeFrame' to not visible
                 welcomeFrame.setVisible(false); 

                 //adds 'logoutTime' to array
                 loginArray.add(logoutTime);

                 //statement that selects everything from our 'Login_Info'
                 String sql7 = "SELECT * FROM Login_Info";

                 //execute query, assigning all records in db to 'rs7'
                 rs7 = st.executeQuery(sql7);                 

                 //moves cursor to next row                                           
                 rs7.moveToInsertRow();

                 //inserts record into db                                     
                 rs7.updateString("User_Name", loginArray.get(0));
                 rs7.updateString("Login_Time", loginArray.get(1));
                 rs7.updateString("Logout_Time", loginArray.get(2));

                 //inserts data into db      
                 rs7.insertRow();               

                 JOptionPane.showMessageDialog(null, "You have successfully logged out!");
              }

              catch(Exception ex)
              {

              }
           }
        });
}

ArrayList是我程序顶部的全局变量。我将减去的两个变量名是 loginTime logoutTime 以及它们在ArrayList中的受尊重索引。

感谢您提供任何指导

1 个答案:

答案 0 :(得分:0)

您可以使用SimpleDateFormat.parse()将字符串转换回日期。一旦它们都是Date格式:

long elapsed = logoutDate.getTime() - loginDate.getTime();  // Elapsed time in ms