Tuesday, February 6, 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) {
e.printStackTrace();
System.out.println("Unable to parse date value " + value);
}
}

0 comments:

Post a Comment