Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

date fomatting question from long

 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using java.sql.Date or java.util.Date?

It seems that either one should give you time as well as the day and date.

Thu Aug 08 12:48:40 MDT 2002

Date date = new Date( yourLong );
System.out.println( date.toString() );

[ August 08, 2002: Message edited by: Marilyn de Queiroz ]
 
Neil Laurance
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example of how to format Dates:

Returns:

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

Cheers, Neil
 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Neil and Jim, that was it.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, don't forget Marilyn. She did try to draw your attention to this issue earlier.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic