2010-09-16 55 views
1

解析proj_close_date時出現錯誤。(java.text.ParseException:Unparseable date:「09/09/2010」) 我正在讀取字符串格式的數據庫中的project_close_date值。我希望它轉換到日期格式發現,是從FROM_DATE TO_DATEJava程序中的解析錯誤

public ArrayList viewAllCustProj1(String frm_date,String to_date,String cust,String proj) 
{ 
    ArrayList list= new ArrayList(); 
    try 
    { 
     String strCust=""; 
     String strproj="";  

     if(!cust.equalsIgnoreCase("ALL") && !cust.equals(null)) 
     { 
      strCust="and customer_code='"+cust+"'"; 
     } 
     if(!proj.equalsIgnoreCase("ALL") && !proj.equals(null)) 
     { 
      strproj="and project_code='"+proj+"'"; 
     }   
     if(cust.equalsIgnoreCase("ALL") && !proj.equalsIgnoreCase("ALL")) 
     { 

     } 
     else 
     { 
      stmt=conn.prepareStatement("select customer_code from mst_customer where visible=1 "+strCust+" and category='EU' and multiple_project=0"); 
      rs=stmt.executeQuery(); 
      while(rs.next()) 
      { 
       reportBean bean=new reportBean(); 
       bean.setCust_code(rs.getString("customer_code")); 
       bean.setProject_code(""); 
       list.add(bean); 
      } 
      rs.close(); 
      stmt.close(); 
     } 


     System.out.println(" select customer_code,project_code,proj_close_date,added_on from mst_project where visible=1 "+strCust+" "+strproj+""); 
     stmt=conn.prepareStatement("select customer_code,project_code,proj_close_date,added_on from mst_project where visible=1 "+strCust+" "+strproj+""); 
     rs=stmt.executeQuery(); 
     while(rs.next()) 
     { 
      reportBean bean=new reportBean(); 

      String proj_close_date=rs.getString(3); 
      String added_on=rs.getString(4); 

      DateFormat myDateFormat = new SimpleDateFormat("MM-dd-yyyy"); 

      DateFormat myDateFormat1= new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); 

      Date myDate1 = null; 
      Date myDate2 = null; 
      Date myDate3 = null; 
      Date myDate4 = null; 
      Date myDate5 = null; 
      try 
      { 
       if(proj_close_date==null || proj_close_date.trim().equals("") || proj_close_date=="NULL") 
       { 
        System.out.println("\n ****** In IF Loop "); 
        bean.setCust_code(rs.getString("customer_code")); 
        bean.setProject_code(rs.getString("project_code")); 
        list.add(bean); 
       } 
       else 
       { 
        System.out.println("\n ****** In Else Loop "); 
        myDate1 = myDateFormat.parse(proj_close_date); 
        myDate2 = myDateFormat.parse(frm_date); 
        myDate3 = myDateFormat.parse(to_date); 
        myDate5 = myDateFormat1.parse(added_on); 

        //myDate4 = myDateFormat.format(myDate5); 

        System.out.println("Project Code ---->"+rs.getString(2));            
        System.out.println("Proj_close_date ------>"+myDate1); 
        System.out.println("From Date ---->"+myDate2); 
        System.out.println("to Date ---->"+myDate3); 
        //System.out.println("Added_on --->"+myDate4); 
        System.out.println("Added_on 1 ie Date 5 ---->"+myDate5); 

        if(myDate1.after(myDate2) && myDate1.before(myDate3)) // means --> if(proj_close_date.after(frm_date) && proj_close_date.before(to_date)) 
        {       
         if(myDate1.after(myDate4)) // means --> if(proj_close_date.after(added_on)) 
         { 
          bean.setCust_code(rs.getString("customer_code")); 
          bean.setProject_code(rs.getString("project_code")); 
          list.add(bean); 
         }    
         else 
         { 
          bean.setCust_code(rs.getString("customer_code")); 
          bean.setProject_code(rs.getString("project_code")); 
          list.add(bean); 
         } 
        }//if  
       }//else 

      }//try 
      catch (ParseException e) 
      { 
       System.out.println("Invalid Date Parser Exception "); 
       e.printStackTrace(); 
      } 


     } 
     rs.close(); 
     stmt.close(); 

    } 
    catch(SQLException sex) 
    { 
     sex.printStackTrace(); 
    } 
    finally 
    { 
     closeConnection(); 
    } 
    return list; 
} 
+1

嗯,這個問題幾乎是完全一樣的http://stackoverflow.com/questions/3724313/date-parsing-from-one-format-to-otherother-format。也許你可以一起做你的作業... – OliBlogger 2010-09-16 07:56:30

回答

3

改變這一行

DateFormat myDateFormat = new SimpleDateFormat("MM-dd-yyyy"); 

之間proj_close_date現在這樣:

DateFormat myDateFormat = new SimpleDateFormat("MM/dd/yyyy"); 

然而,這是相當不清楚爲什麼你將所有的值都作爲字符串,也許你應該考慮專用的ResultSet方法,如getDategetTimeStamp

另一方面,我想提到應該避免通過串聯構建SQL查詢 - 您應該使用?佔位符生成查詢,然後在PreparedStatement上設置參數。