How to insert a date variable in java?

Need help with date format in java?

  • Date currentDateFormat() { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); //get current date time with Date() Date date = new Date(); Date d = dateFormat.format(date); return d; } I am trying to store the value of d in a table, the column is of type Date. I am getting this Error, Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types required: java.util.Date found: java.lang.String at database_entry.MyMethods.currentDateForm… at database_entry.DBEntry.main(DBEntry.java… I do understand that dateFormat.format(date) is a string and should be stored in a string. However, I need to have the same format for a type Date in order for me to be able to store it into the database. If I only use date = new Date(); it does give me the date but in this format, Tue Apr 10 12:22:01 AST 2012 Any ideas?

  • Answer:

    If I only use date = new Date(); it does give me the date but in this format, Tue Apr 10 12:22:01 AST 2012 The above statement is wrong! A data type of Date stores a number. The number of days offset from a base date (possibly 01/01/1970). What you are seeing when you display a date is the system standard format. Change the standard format and your problem is fixed. You do not have to change the Date on the database. Only the format. Have fun.

Bleh22 at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.