1

日付形式 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();
}
4

2 に答える 2

2

myDateFormat1 を に設定しました"yyyy-mm-dd hh:mm:ss.SSSSSS"mm最初は大文字にするべきだと思います。

SimpleDateFormatの場合は、ドキュメントでフォーマット文字列を確認することをお勧めします。

于 2010-09-16T07:28:51.627 に答える
2

いくつかのメモ

  • Java クラス名の慣習は、各名詞を大文字にすることであり、次のようにreportBeanなります。ReportBean
  • SQL 列を位置で参照しないでください。代わりに常に名前を使用してくださいrs.getString("customer_code")rs.getString(3)
  • 意味のある変数名を使用するmyDate1と、closeDate
  • コードのデバッグを練習して、排除できるようにしますSystem.out.println()
  • リソースを適切に解放stmt.close()し、finally ブロック内で移動します
  • 飲み込むのではなく、ロギング フレームワークを使用します。Exceptionlog.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()

于 2010-09-16T08:10:13.463 に答える