Tuesday, April 5, 2011

How to format a date using Apache Derby?

Hello,

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
From stackoverflow
  • 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() + "\"");
        }
    }
    
    luis.espinal : ^^ This. It also goes without saying that you (the OP) do not use Apache Derby (or any relational database system) to format the date. You use a formatter. You use Derby to pull the data with SQL, and you use a formatter to massage the date data and make it pretty.
    Leniel Macaferi : @luis.espinal: really good explanation Luis... :)

0 comments:

Post a Comment