aspose file tools*
The moose likes Java in General and the fly likes date fomatting question from long Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "date fomatting question from long" Watch "date fomatting question from long" New topic
Author

date fomatting question from long

Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
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

Joined: Jul 22, 2000
Posts: 9047
    
  10
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 ]

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

Joined: Jul 18, 2002
Posts: 183
Example of how to format Dates:

Returns:

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

Cheers, Neil
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
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

Joined: Jul 18, 2002
Posts: 183
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Oct 01, 2000
Posts: 167
Thanks Neil and Jim, that was it.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hey, don't forget Marilyn. She did try to draw your attention to this issue earlier.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: date fomatting question from long