Saturday, August 18, 2012

OAF Get Oracle Date FOrmat / Change Date Format

1) From one format to any other format

eg.   15-Aug-2012    to  12/08/15
        String dob;
        dob = "15-Aug-2012 ";
        final String OLD_FORMAT = "dd-MMM-yyyy";
        final String NEW_FORMAT = "yy/MM/dd";
        String  NewDate ;
        NewDate = "";
          if (dob != null) {
            try {
              SimpleDateFormat OldDateFormat = new SimpleDateFormat(OLD_FORMAT);
              Date dt = OldDateFormat.parse(dob);
              OldDateFormat.applyPattern(NEW_FORMAT);
              NewDate = OldDateFormat.format(dt);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
       
        System.out.println("Reqd Date Format is  :" + NewDate);


2) To get Oracle Date Format
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;

        // Inside PR or PFR
        String dob;
        dob = "15-Aug-2012 ";
       // Method Call
       oracle.jbo.domain.Date jboRateStart = castToJBODate(dob);
       System.out.println("JBO Date is  :" + jboRateStart);


      public static  oracle.jbo.domain.Date castToJBODate(String aDate) {
        DateFormat formatter;
        java.util.Date date;
        if (aDate != null) {
            try {
                formatter = new SimpleDateFormat("dd-MMM-yyyy");
                //formatter = new SimpleDateFormat("MM/dd/yyyy");
                date = formatter.parse(aDate);
                java.sql.Date sqlDate = new java.sql.Date(date.getTime());
                oracle.jbo.domain.Date jboDate =
                    new oracle.jbo.domain.Date(sqlDate);
                return jboDate;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

Output
JBO Date is  :2012-08-15

No comments:

Post a Comment