4 messages in com.mysql.lists.javaRe: Formatting mysql timestamps the r...
FromSent OnAttachments
Mads Søgaard05 Mar 2000 11:43 
Paul DuBois05 Mar 2000 11:51 
Tim Endres05 Mar 2000 12:27 
Mads Søgaard05 Mar 2000 14:11 
Subject:Re: Formatting mysql timestamps the right way...
From:Tim Endres (ti@trustice.com)
Date:03/05/2000 12:27:56 PM
List:com.mysql.lists.java

This is a pretty simple matter, but it has caused me quite a few problems, so please help me.

I'm having trouble formatting a date and time the right way. Could anyone please post a snippet of code that formats a mysql timestamp into something readable.

My suggestion is to use the driver's getDate() routine to get a java.sql.Date, which is a subclass of java.util.Date. Then use SimpleDateFormat. For instance:

ResultSet rs = ...; java.sql.Date sqlDate = rs.getDate(1); SimpleDateFormat dFmt = new SimpleDateFormat( "MM/dd/yyyy hh:mm:ss z" ); FieldPosition pos = new FieldPosition(0); StringBuffer fmtBuf = new StringBuffer(); dFmt.format( sqlDate, fmtBuf, pos );

// Now fmtBuf contains your formatted date and time...