In this post I will give examples of converting date from one datatype to another from a Oracle Application Framework perspective. These examples will be useful/handy when you have date value in a specific format and you want to convert/manipulate. I will add more examples when I get more free time :-)
Applicable to ADF Web Application also.
Applicable to ADF Web Application also.
If you have a requirement which is not mentioned here, feel free to ask.
I will be using the below classes 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;
#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.Calendar utilCalendar = Calendar.getInstance();
System.out.println("utilCalendar :" + utilCalendar);
import
oracle.jbo.domain.Date;
import
java.sql.Timestamp;
import
java.util.Calendar;
#3. To print the Current time in DD-MON-YYYY HH24:MI:SS format.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);
import
java.text.SimpleDateFormat;
import
java.text.DateFormat;
import
java.util.Calendar;
Refer: Class SimpleDateFormatDateFormat 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);
#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;
#5. To get the difference between two oracle.jbo.domain.Date in days.
// 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);
import
oracle.jbo.domain.Date;
#6. To convert string 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);
import
java.text.SimpleDateFormat;
import
java.text.DateFormat;
import
oracle.jbo.domain.Date;
#7. Calculate the age based on the Date of Birth from a 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
}
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);
Feel free to point out if anything is missing/wrong in this blog.
No comments:
Post a Comment