You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by zze-MASSON Erwan FTRD/DIH/REN <er...@rd.francetelecom.com> on 2003/05/12 18:01:35 UTC

MySQLDate2JavaUtilDate

Hello,

I had a few problem in MySQL: how to convert mysql Date to a java Date ?(yyyy-MM-dd HH:mm:ss in MySQL format and Sun Mar 23 12:45:30 CET 2003 In java).
I wasn't able to put the hour min:and:sec if I used the normal DATE converter. 
 
After a few search I have make a fieldConversion that works fine.

Enjoy :)

***************************************************************************************************************
You just need to put a few thing in your repository_user.xml and add the class UtilDate2MySQLDATE :)
An example in my repository_user.xml :


!-- Definitions for MyTable -->
   <class-descriptor
   	  class="MyClass"
   	  table="T_MyTable">
      <documentation>
      
		private int id__;
		private String name__;
		private Date dateCreation__;
		      
      </documentation>
	

      <field-descriptor
         name="id__"
         column="Id"
         jdbc-type="INTEGER"
         primarykey="true"
         autoincrement="true"
         nullable="false"
      />
      <field-descriptor
         name="name__"
         column="Name"
         jdbc-type="VARCHAR"
         nullable="false"
         length="50"
      />
      <field-descriptor
         name="dateCreation__"
         column="DateCreation"
         jdbc-type="VARCHAR"
         conversion="UtilDate2MySQLDATE"
         nullable="false"
         length="30"
      />
      
   </class-descriptor>
   




******************************************************************************************************
UtilDate2MySQLDATE.java :


import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
import java.text.SimpleDateFormat;
import java.text.ParsePosition;

/**
 * @author Erwan Masson
 *
 *  
 * 
 */

public class UtilDate2MySQLDATE implements FieldConversion {

	/**
	 * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(Object)
	 */
	public Object javaToSql(Object source) throws ConversionException {
		
        if (source instanceof java.util.Date)
        {
            System.out.println("J2S source: "+source);
            SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
            String the_DateString = df.format(source);
            //String the_DateString = source.toString();
            //System.out.println("After Format: "+the_DateString);
            //java.sql.Date the_Date = new java.sql.Date(0);
            //the_Date.valueOf(the_DateString);
            
            //System.out.println("J2S"+the_Date);
            
            
            return(the_DateString);
            
        }
        else
        {
        	System.out.println("J2SERRROOR"+source.getClass());
            return source;
        }
    
	}

	/**
	 * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(Object)
	 */
	public Object sqlToJava(Object source) throws ConversionException {
		
		if (source instanceof String)
		{	
			//System.out.println("Source in String: "+source);
        	
            SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
			
			//System.out.println("SimpleDateFormat");
			//The space is needed, otherwise the parser doesn't see the first int :)
			java.util.Date the_Date = df.parse(" "+(String)source, new ParsePosition(1));
			
			//System.out.println("SQLTOJAVA: "+the_Date);
			
			return the_Date;
			
        }
        else
        {	
        	//System.out.println("error during the sqltojava:  "+source.getClass());
        	//System.out.println((source).toString());
            return source;
        }
        
        
	}

}