3

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

SELECT recdate FROM myrecords;

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

SELECT MyFormatter(recdate) FROM myrecords
4

1 回答 1

2

看这里:

DateFormat 和 SimpleDateFormat 示例

示例代码:

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() + "\"");
    }
}
于 2010-04-03T21:18:24.987 回答