Tuesday, 6 February 2007

Oracle ADF Accessing the Current Date and Time

This is an extract from section 9.11 of the Oracle ADF Developer's Guide for Forms and 4/GL developers. It provides two functions for getting the current date and the current date and time from within code written inside the BC4J layer.
 * requires import oracle.jbo.domain.Date;
protected Date getCurrentDate()
    return new Date( Date.getCurrentDate());

 * requires import oracle.jbo.Domain.Date and java.sql.Timestamp
protected Date getCurrentDateWithTime()
    return new Date( new Timestamp( System.currentTimeMillis()));
Conventing from a date of type java.util.Date to an oracle.jbo.domain.Date can be achieved using the following technique (the code also demonstrates the use of a date parser to convert arbitrary date formats to valid Java dates.) :
protected static final DateFormat dateFormater = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

public void setDateFromString( String value)
    try {
        java.util.Date javaDate = dateFormater.parse(value);
        Timestamp curTimestamp = new Timestamp( javaDate.getTime());
        oracle.jbo.domain.Date oracleDate = new oracle.jbo.domain.Date(curTimestamp);
        this.setAccidentDate( oracleDate);
    } catch (Exception e) {
        System.out.println("Unable to parse date value " + value);


spido said...
This comment has been removed by the author.
spido said...

Another way to do it, is also:

Calendar aDate = Calendar.getInstance();
java.util.Calendar calDateValue = new java.util.GregorianCalendar();
java.util.Calendar calValue = new java.util.GregorianCalendar();

// calDateValue.setTime(this.getNotifiedDate().getValue());
logger.trace("about to set Date: " + calDateValue.get(Calendar.YEAR) + "/" + calDateValue.get(Calendar.MONTH) + "/" +
calDateValue.get(Calendar.DAY_OF_MONTH) + "/ " + calValue.get(Calendar.HOUR_OF_DAY) + ":" + calValue.get(Calendar.MINUTE));

aDate.set(calDateValue.get(Calendar.YEAR), calDateValue.get(Calendar.MONTH), calDateValue.get(Calendar.DAY_OF_MONTH),
calDateValue.get(Calendar.HOUR_OF_DAY), calDateValue.get(Calendar.MINUTE));

Date oracleDate = null;

oracleDate = new oracle.jbo.domain.Date(new java.sql.Timestamp(aDate.getTime().getTime()));

PS: Obviosly that is a joke :))))