How to format a date using Apache Derby?

无人久伴 提交于 2019-12-10 20:48:49

问题


I would like to format nicely a date received from a query like:

SELECT recdate FROM myrecords;

pratically I am searching the function to pretty formatting with a date pattern, better if SimpleDateFormat like. And if not possible how can I build a class for formatting with somtehing like:

SELECT MyFormatter(recdate) FROM myrecords

回答1:


Look here:

DateFormat and SimpleDateFormat Examples

Sample code:

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() + "\"");
    }
}


来源:https://stackoverflow.com/questions/2572568/how-to-format-a-date-using-apache-derby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!