wood burning stoves
The moose likes Beginning Java and the fly likes Java Datetime issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Datetime issue" Watch "Java Datetime issue" New topic

Java Datetime issue

Mike Boota
Ranch Hand

Joined: Jul 18, 2002
Posts: 82

I am trying to convert the date in "yyyymmddhhMMssSS" format to mill

SimpleDateFormat sdf = new SimpleDateFormat( "yyyymmddhhMMssSS" );
java.util.Date javaDate = sdf.parse( "2007051811531845" );

java.sql.Timestamp ts11 = new java.sql.Timestamp(javaDate.getTime());

Now here when I do System.out I get the following value

Here I am saving the above timestamp in Oracle table
String query_2 = "insert into tsdate values(?)";
PreparedStatement prepstat = dbConnection.prepareStatement( query_2 );
prepstat.setTimestamp( 1, ts11 );
rows = prepstat.executeUpdate();

now when I check the data in oracle table it is saved as

5/18/2011 11:05:19.045000 AM

which is not correct as my original date is 2007 but here it's shown as 2011

And here I am reading it back again from the table:

String selQuery = "Select tsdate from tsdate";
Statement s = dbConnection.createStatement();
ResultSet rs = s.executeQuery(selQuery);
java.sql.Timestamp ts22 = rs.getTimestamp("tsdate");
long tsTime1 = ts22.getTime();
System.out.println("Value is: "+tsTime1); //1305731118045
Calendar cal = Calendar.getInstance();
DateFormat d = DateFormat.getInstance();
SimpleDateFormat sdf2 = new SimpleDateFormat( "yyyymmddhhMMssSS" );

System.out.println(sdf2.format(new Date(tsTime1))); //2011051811051845

Can anyone please tell what I am doing wrong:


MB<br />Sun Certified Programmer for Java2 Platform
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

mm = minutes in hour (05 in your example).

MM = month in year (53 in your example).
I agree. Here's the link: http://aspose.com/file-tools
subject: Java Datetime issue
It's not a secret anymore!