日期从一种格式解析为另一种格式

时间:2010-09-16 06:49:10

标签: java simpledateformat

我想将日期格式yyyy-mm-dd hh:mm:ss.SSS(以字符串格式存储在数据库中)更改为mm / dd / yyyy进行比较

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.SSSSSS");

        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(""))
          {
              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();
}

2 个答案:

答案 0 :(得分:2)

您已将myDateFormat1设置为"yyyy-mm-dd hh:mm:ss.SSSSSS"。我认为第一个mm应该是大写的。

我建议您使用文档SimpleDateFormat检查格式字符串。

答案 1 :(得分:2)

一些注意事项

  • convention用于Java类名,使每个名词大写,reportBean变为ReportBean
  • 不按位置引用SQL列,而是使用名称而不是rs.getString("customer_code")而不是rs.getString(3)
  • 使用有意义的变量名称,myDate1变为closeDate
  • 练习debugging your code,以便您可以取消System.out.println()
  • 优雅地释放资源,stmt.close()在finally块中移动
  • 使用logging framework,而不是吞咽Exception,例如log.error("Invalid Date Parser Exception", e);

一些具体指示:

new SimpleDateFormat("yyyy-mm-dd hh:mm:ss.SSSSSS") // as already noted, mm is the format for minute, MM is the format for month

myDate4 = myDateFormat.format(myDate5); // invalid as you are asigning a String to a Date

if(myDate1.after(myDate4)) // irrelevant as both if & else block execute the same code

rs.close() // not necessary as closed when `Statement` is closed

请参阅Javadoc

您确定数据库架构是全部varchar列吗?如果是这样,我建议您修复它。否则,您可以改为呼叫rs.getDate()

相关问题