I presume your input is a String representing a date in yyyy-MM-dd format and you want your output to be a String of that same date, but in MM/dd/yyyy format. In that case, you'll need two SimpleDateFormat objects--one to parse the yyyy-MM-dd String into a Date object and another to format that Date object into a MM/dd/yyyy String.
As others already mentioned, a date object doesn't have a format. The purpose of the simple date format class is to format or parse the format, when it is converted to or from a string. All the formatting is on the string, the date object doesn't have any formatting.
1.My input is date in yyyy-MM-dd format.
Acutally, no. You "date" is a integer, which is valued at 1995.
2.I convertes that into String using format function. now the result is string which contains date in MM/dd/yyyy format.
Which I am surprised actually compiled, as there isn't a format() method that takes an int. But yes, assuming that you have a properly formatted string, you can get the date with the parse() method.
3.Now I convert that String to date,so I used parse function but the result is in different format.
Again, a date object doesn't have a format. Once it is a date, you need to format() it with the simple date format, to get back a string, if you want to print it.
Henry Wong wrote:Which I am surprised actually compiled, as there isn't a format() method that takes an int.
Format has a method format(Object), so the int is autoboxed into an Integer.
This method calls format(Object, StringBuffer, FieldPosition) which is overridden (implemented actually) by DateFormat with the following body:
So not only will it compile, it will even run! The results will not be what you'd expect though.