2014-04-14 4 views
0

사용자가 로그인 할 때 응용 프로그램에 액세스 할 수있는 프로그램 내부에 사용자가 로그인 할 때 타임 스탬프를 가져 와서 ArrayList에 저장하는 부분이 있습니다. 사용자가 로그 아웃 할 때 로그 아웃 할 때 다른 타임 스탬프가 찍혔습니다. 나는 서로에게서 2 번을 빼내려고 노력해 왔지만 그것이 나를 허용하지는 않는다. 그들은 String ArrayList에 저장되어 있습니다. String을 감할 수 없다는 것을 알고 있습니다. 그래서 double, int, date 등으로 변환하려고 시도했습니다. 아무 것도 작동하지 않습니다. 내 질문은 두 번 차이가 나는 것입니다. 나는 또한 이와 비슷한 다른 질문이 있음을 알고 있지만이 정확한 질문에만 국한된 것은 없다. 그래서 나는 그것을 게시했다. 나는 사본을 게시하려고하지 않는다.ArrayList의 항목을 다른 유형으로 변환

코드 :

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에 내 프로그램의 상단에있는 전역 변수입니다. 내가 뺄 것입니다 두 변수 이름은 loginTimelogoutTime 및 ArrayList 내에서 그들의 존경받는 인덱스입니다. 대한 의견

감사

+0

당신은 디자인 문제가있다 : 당신은 인/아웃 타임 스탬프에 로그의 목록에 *'Strings' *를 저장하지 않아야합니다. 본질적으로 타임 스탬프 인'Date' 객체를 저장해야합니다. 그런 다음 각 날짜 쌍의 시간을 뺄 수 있습니다. – Bohemian

+0

@javaGeek 너무 많은 코드가 표시됩니다. 문제가되는 코드의 관련 부분을 추출하는 것은 사용자의 임무입니다. 당신은 날짜 - 시간 값의 ArrayList에 대해 질문하고 있습니다. 따라서 데이터베이스 나 JOptionPane에서 암호를 쿼리 할 필요가 없습니다. 또한, 귀하의 질문은 다르다 (복제본이 아님)하지만 귀하는 그것이 특별한 이유를 설명하는 것을 게을리합니다. (a) 날짜 - 시간 값을 데이터베이스에 쿼리하는 방법, (b) 문자열을 날짜 - 시간 값으로 파싱하는 방법, 그리고 (c) 경과 시간을 계산하는 방법과 같은 반복적 인 질문과 같습니다. –

+0

@BasilBourque 아니오, C는 내가 가진 유일한 질문입니다. 읽으면 이미 해당 유형을 구문 분석하고 데이터베이스를 쿼리하는 방법을 언급하지 않았습니다. – javaGeek

답변

0

당신은 당신의 SimpleDateFormat.parse()를 사용하여 날짜로 다시 문자열을 변환 할 수 있습니다. 그들이 Date 형식으로 모두 일단 :

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