Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Datetime issue

 
Mike Boota
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
//1305731118045


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:

Thanks
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mm = minutes in hour (05 in your example).

MM = month in year (53 in your example).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic