如何使用Apache Derby格式化日期?

时间:2010-04-03 20:28:56

标签: java date derby

我想很好地格式化从查询收到的日期,如:

SELECT recdate FROM myrecords;

实际上我正在使用日期模式搜索函数以进行相当格式化, 如果SimpleDateFormat喜欢更好。如果不可能,我如何构建一个格式化的类 像somtehing一样:

SELECT MyFormatter(recdate) FROM myrecords

1 个答案:

答案 0 :(得分:2)

看这里:

DateFormat and SimpleDateFormat Examples

示例代码:

public static void main(String[] args)
{
    // Get the Date object that comes from DerbyDB...
    //Date derbyDate = YOUR DATE FIELD HERE

    // Make a SimpleDateFormat for toString()'s output. This
    // has short (text) date, a space, short (text) month, a space,
    // 2-digit date, a space, hour (0-23), minute, second, a space,
    // short timezone, a final space, and a long year.
    SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");

    // See if we can parse the output of Date.toString()
    try
    {
        Date parsed = format.parse(derbyDate.toString());

        System.out.println(parsed.toString());
    }
    catch(ParseException pe)
    {
        System.out.println("ERROR: Cannot parse \"" + derbyDate.toString() + "\"");
    }
}