Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a date using Apache Derby?

Tags:

java

date

derby

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
like image 842
Steel Plume Avatar asked Dec 07 '25 04:12

Steel Plume


1 Answers

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() + "\"");
    }
}
like image 174
Leniel Maccaferri Avatar answered Dec 12 '25 05:12

Leniel Maccaferri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!