aspose file tools*
The moose likes JDBC and the fly likes formatting a SQL date in MS Access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "formatting a SQL date in MS Access" Watch "formatting a SQL date in MS Access" New topic
Author

formatting a SQL date in MS Access

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jul 09, 2001
Posts: 1879

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: formatting a SQL date in MS Access
 
Similar Threads
Insert a row into MS Access2003 successfully,But the long date(YYYY/MM/DD HH:mm:ss) was changed
Converting MySQL Datetime to MS SQL Datetime and vice versa !
date conversion
Comparing two Dates
string to date conversion