Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 24th, 2003, 05:02 PM
Registered User
 
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to nevada city
Default Timestamp and sql

I did
String myoldts = new String("2003-05-23 18:19:20");
Timestamp myts = new Timestamp(0L);
myts.valueOf(myoldts);

System.out.println(myts.toString());

1969-12-31 19:00:00.0

How can I convert a String with a SQL timestamp to write to a sql database ?
 
Old June 24th, 2003, 09:11 PM
Registered User
 
Join Date: Jun 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It depends on what format does your DB supports or used to store date value. E.g. for DB/2, the default date format is MM/DD/YYYY so the way to format your data would need a use of SimpleDateFormat() and use a Calendar() object to set the date and time to convert to the proper format.

Look at the following Calendar to DB/2 date format function of mine:

Code:
public final String SQLCalFieldFormat(Object value) {

        if ((value != null)
            && ((value.getClass().equals(GregorianCalendar.class)
                || (value.getClass().equals(Calendar.class))))) {
            Calendar nv = (Calendar) value;
            SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
            return "'" + formatter.format(nv.getTime()) + "'";

        } else {
            return "NULL";
        }
}
Hope this helps!

Neo Gigs

Thanks

Neo Gigs
 
Old June 24th, 2003, 10:06 PM
Registered User
 
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to nevada city
Default

Thanks for the info, I am using or trying to use firstsql (1% java database)

However any idea why I got 1969 ?

Thanks
 
Old June 24th, 2003, 11:20 PM
Registered User
 
Join Date: Jun 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dun use TimeStamp. use a Calendar object to set the date you wanto hold and stores it into DB. Check doc API for the proper definition and limitation of using TimeStamp.

Thanks

Neo Gigs





Similar Threads
Thread Thread Starter Forum Replies Last Post
TimeStamp nay_ctg BOOK: Beginning Cryptography with Java 1 August 7th, 2008 07:37 PM
File name with timestamp bhushanam BOOK: Professional SQL Server 2005 Reporting Services ISBN: 0-7645-8497-9 1 September 28th, 2007 04:39 AM
GridView + timestamp victorsj ASP.NET Espanol 0 August 29th, 2006 10:43 AM
timestamp isheikh BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 November 18th, 2004 06:23 PM
UNIX Timestamp apek PHP How-To 4 January 20th, 2004 02:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.