• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

date fomatting question from long

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Neil and Jim, that was it.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, don't forget Marilyn. She did try to draw your attention to this issue earlier.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic