【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
}
}
本文介绍了如何在业务逻辑中获取并使用当前日期和时间的方法,并提供了两个示例方法:一个用于获取不含时间信息的当前日期,另一个用于获取包含时间信息的当前日期。此外,还详细展示了oracle.jbo.domain.Date类的使用方法,包括如何将字符串转换为Date对象。
1299

被折叠的 条评论
为什么被折叠?



