0

我正在执行查询

select level as VERSION,
name as DESCRIPTION,code as CODE,created as RELEASEDATE
,SUBSTR(name, INSTR(name, '.',-1)+1) as TYPE
from mytable
where code = 'HX56UO'

这给出了类似的结果

Type    Version     Description       Release Date   
.exe    3.0.2   MPC560xB MCAL Autosar 3.x    15sep2011_00:00:00

我需要像 2011 年 9 月 15 日这样的发布日期,我需要在 bean set 方法中设置这个结果值。

public void setLastDate(java.util.Calendar newLastPubDate) {

    lastDate = newLastDate;

}

由于设置方法是日历,我需要从结果集中设置上述值的类型转换。我试着像这样设置值

if (rs.getDate("RELEASEDATE") != null ) {
Bean.setLastPubDate(rs.getdate"RELEASEDATE"));
}

但它不是日期格式,它的日历如何设置值。请帮我解决这个问题。谢谢。

4

2 回答 2

3
if (rs.getDate("RELEASEDATE") != null ) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(rs.getDate("RELEASEDATE"));
    Bean.setLastPubDate(calendar);
}

编辑:当您以后想要将日历转换为字符串(以显示它)时,您可以使用SimpleDateFormat

String formatted = new SimpleDateFormat("dd MMMM yyyy").format(bean.getLastPubDate().getTime());
于 2012-06-15T11:36:10.953 回答
2

As your created column is a VARCHAR2 - very bad practice to not store a date as a DATE but we'll skip that for now - you need to convert it to a date type somewhere, and in the query is possibly simplest:

select level as VERSION,
    name as DESCRIPTION,
    code as CODE,
    TO_DATE(created,'DDMONYYYY_HH24:MI:SS') as RELEASEDATE,
    SUBSTR(name, INSTR(name, '.',-1)+1) as TYPE
from mytable
where code = 'HX56UO'

You can still use getDate() from the resultset, and need to convert that Date to a Calendar object as you call your bean method:

Bean.setLastPubDate(Calendar.getInstance().setTime(rs.getDate("RELEASEDATE"));
于 2012-06-15T11:36:34.013 回答