1

java datetime (date.getTime()) is stored as string in mysql field. How can we convert this to sql date using sql query. I am using mysql database. Is there any sql function available? For example - This is stored (1416231812348) for today's date in db.

Thanks for suggestions.

4
  • where 1416231812348 is stored. mysql datetime field?? Commented Nov 17, 2014 at 13:53
  • No, as string it is stored (varchar). Commented Nov 17, 2014 at 13:55
  • possible duplicate of A datetime equivalent in java.sql ? (is there a java.sql.datetime ?) Commented Nov 17, 2014 at 17:44
  • No, it is no duplicate of that.It is not java related question, but mysql query. Commented Nov 18, 2014 at 5:46

3 Answers 3

1

Java is returning the date as a long, to convert it you can use:

SELECT FROM_UNIXTIME(event_time) FROM MY_TABLE

If you get an error, try the following (after testing, I can see that your data is stored in milliseconds so you need to use this method):

SELECT FROM_UNIXTIME(event_time/1000) FROM MY_TABLE

(Change event_time to be the field name in your table and MY_TABLE to be the table name.)

Here is a SQLFiddle example that shows it working.

Here is an answer that gives you formatting options as well: http://notsoyellowstickies.blogspot.co.uk/2011/11/converting-long-into-datetime-mysql.html

Sign up to request clarification or add additional context in comments.

Comments

0

There is a java.sql package, that has time included. You can send it straight into your database without needing to convert it.

This may be a more pre-emptive solution than converting a date string from Java, into time in MySQL.

A similar question was answered and may be able to help you out here:

A datetime equivalent in java.sql ? (is there a java.sql.datetime ?)

Comments

0

most probably you have recorded from:

System.currentTimeMillis()

so:

select DATE_FORMAT ( from_unixtime( your_table_field / 1000 ) , '%e %b %Y');

you can change the date format as you like.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.