0

I want to filter orders based on dates. My code is,

    if ( filterObject != null ) {
        if ( !AgileUtil.isEmpty( filterObject.getCustid() ) ) {
            query.append( "AND ORD.CUSTID = '" ).append( filterObject.getCustid() ).append( "' " );
        }
        if ( !AgileUtil.isEmpty( filterObject.getPid() ) ) {
            query.append( "AND ORD.OID IN ( SELECT OD.OID FROM ORDERDETAIL OD WHERE ORD.OID = OD.OID AND OD.PID = '" )
            .append( filterObject.getPid() )
            .append( "' ) " );
        }
        if ( filterObject.getFromDate() != null ) {
            //AppConstants.DATEFORMAT.format( obj.getCreated() );

            query.append( "AND ORD.OID IN(SELECT OD.OID FROM ORDERDETAIL OD WHERE ORD.OID = OD.OID AND OD.CREATED > FROMDATE '" )
            .append( filterObject.getFromDate() )
            .append( "' " );

        }
        if ( filterObject.getToDate() != null ) {

            query.append( "AND ORD.OID IN(SELECT OD.OID FROM ORDERDETAIL OD WHERE ORD.OID = OD.OID AND OD.CREATED < TODATE '" )
            .append( filterObject.getToDate() )
            .append( "' " );
        }
        if ( ( filterObject.getFromDate() != null ) && ( filterObject.getToDate() != null ) ) {
            query.append( "AND ORD.OID IN(SELECT OD.OID FROM ORDERDETAIL OD WHERE ORD.OID = OD.OID AND OD.CREATED BETWEEN FROMDATE AND TODATE '" )
                .append( filterObject.getFromDate() );
        }
        if ( filterObject.getAmount() != null ) {
            query.append( "AND ORD.TOTAL = " ).append( filterObject.getAmount() );
        }
    }

Now the main question in front of me is how can I convert date like 31/08/2015 to mysql format 0000-00-00 and pass it to filterObject.getFromDate(),filterObject.getToDate() for comparison this date with table column 'CREATED' in database.

SaviNuclear
  • 886
  • 1
  • 7
  • 19
Vilas
  • 837
  • 2
  • 15
  • 41
  • 4
    [Use Prepared Statements](http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html) and `java.sql.Date` and let the driver deal with it – MadProgrammer Sep 03 '15 at 05:25
  • Google link : [convert java date to mysql date](https://www.google.co.in/search?q=convert+java+date+to+mysql+date&ie=utf-8&oe=utf-8&gws_rd=cr&ei=ytnnVdPvBtWNuASk1LtI) – Naman Gala Sep 03 '15 at 05:27
  • 2
    Dude. First google for 'SQL Injection Attack', then chuckle at http://xkcd.com/327/, then figure out how to do this with a PreparedStatement which allows you to just use a Java Date object directly independent of whatever database you are on. – Bob Kuhar Sep 03 '15 at 05:34
  • @BobKuhar Thank you. That xkcd made my day. – Andreas Sep 03 '15 at 05:36

0 Answers0