Java程序中的解析错误

发布于 2024-09-19 01:53:25 字数 4819 浏览 5 评论 0原文

错误发生在 proj_close_date 的解析附近。( java.text.ParseException: Unparseable date: "09/09/2010" ) 我正在从数据库中读取字符串格式的project_close_date值。我想将其转换为日期格式以查找 proj_close_date 是否存在于 from_date 和 to_date 之间

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;
}

the error occur near the parsing of proj_close_date.( java.text.ParseException: Unparseable date: "09/09/2010" )
i am reading project_close_date value from database which is in string format. i want convert it in to date format to find that, is proj_close_date present between from_date and to_date

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2024-09-26 01:53:25

将此行更改

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

为:

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

但是,目前还不清楚为什么将所有值作为字符串获取,也许您应该考虑专用的 ResultSet 方法,例如 getDategetTimeStamp< /代码>。

另一方面,我想提一下,应该避免通过串联构建 SQL 查询——您应该使用 ? 占位符生成查询,然后在 PreparedStatement

Change this line

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

to this:

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

However, it's quite unclear why you get all the values as strings, perhaps you should consider dedicated ResultSet methods such as getDate or getTimeStamp.

As another side remark I'd like to mention that building SQL queries by concatenation should be avoided -- you should generate queries with ? placeholders, and then set the parameters on your PreparedStatement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文