• 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

formatting a SQL date in MS Access

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I format a SQL date in a Java program that uses a MS Access DB ???
I need to get it like:
DD/MM/YYYY
instead of MM/DD/YYYY
any ideas or code would be helpful
Thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Suhail,
its very simple, this is how I do it:
StringTokenizer tokens = new StringTokenizer( res.getDate(c++).toString(), "-" );
String year = tokens.nextToken();
String month = tokens.nextToken();
String day = tokens.nextToken();
String d = month + "/" + day + "/" + year;
now you could just write, myJTextField.setText( d );
hope this will be some help, tell me if it helped you.
Yoel
------------------
Sun Certified Programmer for JAVA 2 Platform
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yoel,
At the moment my code looks like the following:
TimeZone gmt = TimeZone .getDefault();
GregorianCalendar inauguration = new GregorianCalendar(gmt);
SimpleDateFormat sdf = new SimpleDateFormat( "EEE dd/MM/yyyy hh:mm:ss aa " );

sdf.setCalendar(inauguration );
String theDate = sdf.format(inauguration .getTime());
try
{
// set timestamp
Date date = sdf.parse (theDate);
inauguration.setTime(date );
java.sql.Date sdate = new java.sql.Date(date.getTime());
PreparedStatement ps = connection.prepareStatement("INSERT INTO test " +
"VALUES (?)");
ps.setDate(1,sdate);
ps.executeUpdate();
ps.close();
As seen above I can format a java.util.date and insert into a text based cell in a table. I can also create a SQL date nad insert that into a date type cell, but its just the formatting.
How would your code work with mine ?
Suhail.

Originally posted by yoel stern:
Hi Suhail,
its very simple, this is how I do it:
StringTokenizer tokens = new StringTokenizer( res.getDate(c++).toString(), "-" );
String year = tokens.nextToken();
String month = tokens.nextToken();
String day = tokens.nextToken();
String d = month + "/" + day + "/" + year;
now you could just write, myJTextField.setText( d );
hope this will be some help, tell me if it helped you.
Yoel


 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it doesn't really matter how it is stored in the database, does it? why can't you just format it when you retrieve it either using SimpleDateFormat in your program or by using the database function format(x,'') function in access?
I'm not really clear as to what you are trying to accomplish and why . It seems like you know how to use SimpleDateFormat from your example!!??
>"I can also create a SQL date nad insert that into a date type
>cell, but its just the formatting."
Formatting should be left independant of the database. when you retrieve the value you can format it the way you like!

Jamie
 
Hot dog! An advertiser loves us THIS much:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic