date fomatting question from long

Rob Levo

Ranch Hand

Posts: 167

posted 15 years ago

I have a date of when a file was last updated, so it is a long, and I need to put it in a Sybase database as a datetime field. I used the toString method of the Date object to get a string, and it worked, but it did not give me the time, only the date, so when I put it into the database, the time defaulted to 12:00AM. I need to create a string that has the date and time, and I do not see a method for the Date class that does that. Can anyone help here, Thanks in advance! Rob

JavaBeginnersFaq"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

Neil Laurance

Ranch Hand

Posts: 183

posted 15 years ago

Example of how to format Dates:

Returns: 20020808193125 19700101000000 08-August-2002 01-January-1970

Cheers, Neil

Rob Levo

Ranch Hand

Posts: 167

posted 15 years ago

I am getting: 2002-08-12 from: long file_time = file_name.lastModified(); Date date = new Date( file_time ); System.out.println( date.toString() ); I do not know why I am not getting the time along with the date. Thanks in advance!!

Neil Laurance

Ranch Hand

Posts: 183

posted 15 years ago

Hmm, according to here, you should see an output of the form: dow mon dd hh:mm:ss zzz yyyy Is your Date a java.util.Date? Check the imports for your code?

Jim Yingst

Wanderer
Sheriff

Posts: 18671

posted 15 years ago

You've got a java.sql.Date, not a java.util.Date. The API specifies the format of toString() for each type; you've got the former. Probably you should either replace "import java.sql.*;" with a series of specific class imports (not including java.sql.Date) or replace every "Date" with "java.util.Date".

"I'm not back." - Bill Harding, Twister

Rob Levo

Ranch Hand

Posts: 167

posted 15 years ago

Thanks Neil and Jim, that was it.

Jim Yingst

Wanderer
Sheriff

Posts: 18671

posted 15 years ago

Hey, don't forget Marilyn. She did try to draw your attention to this issue earlier.