I have a date retrieved from a sql database (example : 2013-08-22 00:00:00.000). I need to convert this to another format (example: Aug-2013).
2013-08-22 00:00:00.000 -> Aug-2013
How do I do this using java?
I have a date retrieved from a sql database (example : 2013-08-22 00:00:00.000). I need to convert this to another format (example: Aug-2013).
2013-08-22 00:00:00.000 -> Aug-2013
How do I do this using java?
Try this (If you wish to format the date the way you want):
Suppose the String variable containing the date is "date"...
String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int index = date.subString(5,6);
String year = date.subString(0,3);
date = months[index-1] + "-" + year;
If your date is retrieved from the database, then you should retrieve it using .getDate
method of the ResultSet
. That way you're getting a proper Date
object, which you can format any way you like.
If it comes from elsewhere as a string, then you can create a SimpleDateFormat
object with the specific format you need and use parse
method to get the Date
object. Then, again, you can format it any way you like.
Get the number of seconds since a date in the past such as January 1st 1970.
SELECT DATEDIFF(s,'1970-01-01','2013-08-22') AS seconds
Then you can take this value and add it to the same date in Java, or any other language for that matter.
This will be a reliable way to get around regional differences in date formats.