Tuesday, May 12, 2020

Playing with Dates in Oracle Application Framework (OAF)

//Playing with Dates in Oracle Application Framework (OAF)

//below classes are used in the examples:

oracle.jbo.domain.Date;
oracle.jbo.domain.Timestamp;
java.sql.Date;
java.sql.Timestamp;
java.util.Date;
java.util.Calendar;
java.text.DateFormat;
java.text.SimpleDateFormat;
java.lang.String;

//1. Get the current Date time.

    Using java.util.Date.

 import java.util.Date; 

 Date utilDate = new Date();  
 System.out.println("utilDate :" + utilDate);  

    Using oracle.jbo.domain.Date.

 import java.sql.Timestamp;                  
 import oracle.jbo.domain.Date; 

 Timestamp sqlTimestamp = new Timestamp(System.currentTimeMillis());   
 Date jboDomainDate = new Date(sqlTimestamp); 
 System.out.println("jboDomainDate :" + jboDomainDate); 

    Using java.util.Calendar.

 import java.util.Calendar; 

 Calendar utilCalendar = Calendar.getInstance();  
 System.out.println("utilCalendar :" + utilCalendar); 

//2. Get the current Date time in oracle.jbo.domain.Date and add 10 hours to that and get the time back in oracle.jbo.domain.Date along with the time component.

 import oracle.jbo.domain.Date; 
 import java.sql.Timestamp; 
 import java.util.Calendar; 

 Timestamp datetime = new Timestamp(System.currentTimeMillis());  
 Date jboDomainStartDate = new Date(datetime);  
 System.out.println("jboDomainDate :" + jboDomainStartDate);
 
 Calendar utilCalendar = Calendar.getInstance();  
 utilCalendar.setTime(jboDomainStartDate.getValue());  
 System.out.println("utilCalendar Start:" + utilCalendar.getTime());
  
 int hoursToAdd = 10;  
 utilCalendar.add(Calendar.HOUR, hoursToAdd);  
 System.out.println("utilCalendar End:" + utilCalendar.getTime());
  
 Timestamp sqlTimeStampEnd = new Timestamp(utilCalendar.getTime().getTime());  
 Date jboDomainEndDate = new Date(sqlTimeStampEnd);  
 System.out.println("jboDomainEndDate :" + jboDomainEndDate);
 

//3. To print the Current time in DD-MON-YYYY HH24:MI:SS format.

 import java.text.SimpleDateFormat; 
 import java.text.DateFormat; 
 import java.util.Calendar; 

 DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); 
 Calendar cal = Calendar.getInstance();
 String dateStr = dateFormat.format(cal.getTime());   
 System.out.println("Current Time : " + dateStr); 

Refer: Class SimpleDateFormat
//4. To get the oracle.jbo.domain.Date in DD-MON-YYYY HH24:MI:SS format.

 import java.text.SimpleDateFormat; 
 import java.text.DateFormat; 
 import java.util.Calendar; 

 // This is a sample code to initialize the domain Date
 Timestamp datetime = new Timestamp(System.currentTimeMillis());  
 Date jboDomainDate = new Date(datetime);
        
 DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); 
 String dateStr = dateFormat.format(jboDomainDate.getValue());  
 System.out.println("Current Time : " + dateStr); 

//5. To get the difference between two oracle.jbo.domain.Date in days.

 import oracle.jbo.domain.Date; 

 Date jboDomainStartDate = null; 
 Date jboDomainEndDate  = null;

 // Code to get the date Values has to be written here
 
 int diffDays = (int)((jboDomainEndDate.getValue().getTime() - jboDomainStartDate.getValue().getTime()) / (1000 * 60 * 60 * 24)); 
 System.out.println("Diff in days:"+ diffDays); 

//6. To convert string oracle.jbo.domain.Date.

 import java.text.SimpleDateFormat;  
 import java.text.DateFormat; 
 import oracle.jbo.domain.Date; 

 try 
 { 
      DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy"); 
      java.sql.Date sqlDate = new java.sql.Date(dateFormat.parse("10-JAN-2010").getTime());  
      Date jboDomainDate = new Date(sqlDate); 
 }catch(Exception e) { 
      System.out.println(e.getMessage()); 
      // throw appropirate Error Message 
 } 

//7. Calculate the age based on the Date of Birth from a oracle.jbo.domain.Date.

  import oracle.jbo.domain.Date;  
  import java.util.Calendar; 

  Date doB = null; 
  // get the DOB and populate the doB variable 
    
  Calendar dob = Calendar.getInstance(); 
  dob.setTimeInMillis(doB.getValue().getTime()); 
    
  Calendar sysDate = Calendar.getInstance(); 
    
  int age = sysDate.get(Calendar.YEAR) - dob.get(Calendar.YEAR); 
    
  if ((dob.get(Calendar.MONTH) > sysDate.get(Calendar.MONTH)) 
       ||  
      (dob.get(Calendar.MONTH) == sysDate.get(Calendar.MONTH) &&  
           dob.get(Calendar.DAY_OF_MONTH) > sysDate.get(Calendar.DAY_OF_MONTH))) { 
    age--; 
  } 
  System.out.println("Age:" + age); 

 //8. Calculate the difference between two Dates in OAF


Date StartDate= (Date)CurrentRow.getAttribute("StartDate");
    Date CurrentDate = (Date)am.getOADBTransaction().getCurrentDBDate();
    java.sql.Date sdate = (java.sql.Date) StartDate.dateValue();
    java.sql.Date cdate = (java.sql.Date) CurrentDate.dateValue();
    int days = daysBetween(sdate,cdate );

  public int daysBetween(java.sql.Date d1, java.sql.Date d2){
     return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
         }

No comments:

Post a Comment