20 March, 2012

Date Classes Conversion in ADF


In ADF we can use three data types classes of Date
oracle.jbo.domain.Date
java.util.Date
java.sql.Date

Note oracle.jbo.domain.Date class extends  java.sql.Date class 

Many ADF developers sometimes face exception when assigning or casting different Date classes to another, It will raise exception ClassCastException

So I will present a methods to convert between different Date classes
I add main method to test Date Conversion between different class.
I add an object for every class and assign it to current date of system.



 public class DateConversion {  
   public static oracle.jbo.domain.Date getJboDateFromUtilDate(java.util.Date utilDate) {  
     oracle.jbo.domain.Date jboDate = null;  
     if (utilDate != null) {  
       jboDate = new oracle.jbo.domain.Date(DateConversion.getSqlDateFromUtilDate(utilDate));  
     }  
     return jboDate;  
   }  
   public static oracle.jbo.domain.Date getJboDateFromSqlDate(java.sql.Date sqlDate) {  
     oracle.jbo.domain.Date jboDate = null;  
     if (sqlDate != null) {  
       jboDate = new oracle.jbo.domain.Date(sqlDate);  
     }  
     return jboDate;  
   }  
   public static java.sql.Date getSqlDateFromJboDate(oracle.jbo.domain.Date jboDate) {  
     return jboDate.dateValue();  
   }  
   public static java.sql.Date getSqlDateFromUtilDate(java.util.Date utilDate) {  
     return new java.sql.Date(utilDate.getTime());  
   }  
   public static java.util.Date getUtilDateFromJboDate(oracle.jbo.domain.Date jboDate) {  
     java.util.Date utilDate = null;  
     if (jboDate != null) {  
       utilDate = new java.util.Date(jboDate.dateValue().getTime());  
     }  
     return utilDate;  
   }  
   public static java.util.Date getUtilDateFromSqlDate(java.sql.Date sqlDate) {  
     return new java.util.Date(sqlDate.getTime());  
   }  
   public static void main(String[] args) {  
     //#1 : Assign every Date to current Date  
     oracle.jbo.domain.Date jboDate = new oracle.jbo.domain.Date(oracle.jbo.domain.Date.getCurrentDate());  
     java.util.Date utilDate = new java.util.Date(); //automatically constructor get current date  
     java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());  
     //#2 : Test whole method in Class  
     //Test getJboDateFromUtilDate  
     jboDate = DateConversion.getJboDateFromUtilDate(utilDate);  
     System.out.println(jboDate.toString());  
     //Test getJboDateFromSqlDate  
     jboDate = getJboDateFromSqlDate(sqlDate);  
     System.out.println(jboDate.toString());  
     //Test getSqlDateFromJboDate  
     sqlDate = getSqlDateFromJboDate(jboDate);  
     System.out.println(sqlDate.toString());  
     //Test getSqlDateFromJboDate  
     sqlDate = getSqlDateFromUtilDate(utilDate);  
     System.out.println(sqlDate.toString());  
     //Test getSqlDateFromJboDate  
     utilDate = getUtilDateFromJboDate(jboDate);  
     System.out.println(utilDate.toString());  
     //Test getSqlDateFromJboDate  
     utilDate = getUtilDateFromSqlDate(sqlDate);  
     System.out.println(utilDate.toString());  
   }  
 }  

Conclusion
Choose which Date Class you will use commonly in your application and unify date class everywhere in your application.
I prefer oracle.jbo.domain.Date Class that is provided by ADF framework.
You can use java.text.SimpleDateFormat for patterning date format.

Thanks
Mahmoud Ahmed El-Sayed
Recommended Post Slide Out For Blogger