Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

formatting a SQL date in MS Access

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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

 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic