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
posted
0
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
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
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!