您的位置:首页 > 其它

Date and Time in ADF

2009-05-15 21:26 337 查看
【1】、How to Access the Current Date and Time
You might find it useful to reference the current date and time in your entity object
business logic. Example 9–17 shows a helper method you can use to access the current
date without any time information.

Example 9–17 Helper Method to Access the Current Date with No Time
/*
* Helper method to return current date without time
*
* Requires import: oracle.jbo.domain.Date
*/
protected Date getCurrentDate() {
return new Date(Date.getCurrentDate());
}

Example 9–18 Helper Method to Access the Current Date with Time
/*
* Helper method to return current date with time
*
* Requires imports: oracle.jbo.domain.Date
* java.sql.Timestamp
*/
protected Date getCurrentDateWithTime() {
return new Date(new Timestamp(System.currentTimeMillis()));
}

【2】、oracle.jbo.domain.Date用法小结
oracle.jbo.domain.Date继承了oracle.sql.DATE,具有方法public java.sql.Date dateValue()。测试例子如下:

package test;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import java.util.GregorianCalendar;

import oracle.jbo.domain.Date;

public class TestDate {
public TestDate() {
}

private Date str2Date(String strDate) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
java.util.Date date = sdf.parse(strDate);
Timestamp timestamp = new Timestamp(date.getTime());
Date returnDate = new Date(timestamp);

return returnDate;
}

//测试带时间的日期
public void testTime(){
try {
Date d = str2Date("2005-10-31 15:46");
System.out.println("2005-10-31 15:46 TO:" + d);

} catch (Exception ex) {
ex.printStackTrace();
}
}

//测试不带时间的日期
public void testDateWithoutTime(){
GregorianCalendar gc = new GregorianCalendar();
gc.set(2005,11,1); //2005年12月1日。0:1月;1:2月 以此类推。
java.util.Date date = gc.getTime();
Date returnDate = new Date(new java.sql.Date(date.getTime()));
System.out.println(returnDate);
}

public static void main(String[] args) {
TestDate testDate = new TestDate();
testDate.testTime();
//OUTPUT: 2005-10-31 15:46 TO:2005-10-31 15:46:00.0
testDate.testDateWithoutTime();
//OUTPUT: 2005-12-01
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: