Dont format the date by hand!!! Use a PreparedStatement to write to the database, it handles the formatting for you. e.g.:
long curTime = new java.util.Date().getTime(); Connection connection= ... ; PreparedStatement pstmt = connection.prepareStatement( "update bla set time = ?"); pstmt.setDate( 1 ,new java.sql.Date( curTime )); //... > > I am javing a jsp file where I would like to get the current date and > time to be inserted in fields of an SQL table. The date should go into > the MySQL date field and hence should be of format yyyy-mm-dd. I tried > to use java.sql.Date, but I couldn't get it to work. If I simply use > > long date = 0; > java.sql.Date sqlDate = new java.sql.Date(date); > int year = sqlDate.getYear(); > > I get back "70". So obviously I have to get the year from the machine's > time, which is a linux box with Redhat 7.3. > > So I tried this: > > long date = 0; > java.sql.Date sqlDate = new java.sql.Date(date); > String thetime = sqlDate(System.currentTimeinMillis()); > out.println ("SQL time is " + thetime); > > But this didn't work either. > > How do you do this? > > In addition, I would like to get the time, but in decimal format i.e. > rather than 11:15 I would like to get 11.25 > > Is there a way of doing this? > > Any help will be greatly appreciated. > > Thanks > > Hugo ==========================================================================To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant FAQs on JSP/Servlets can be found at: http://archives.java.sun.com/jsp-interest.html http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.jsp http://www.jguru.com/faq/index.jsp http://www.jspinsider.com