What I want to achieve is to convert a date in format yyyyMMdd
to locale format i.e yyyy/MM/dd
or dd/MM/yyyy
etc.
I am not interested in the time part, I just require date.
The function would take string date and return a string date in locale format.
What I currently have is:
dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
convertedDate = dateFormat.parse("20120521");
Everything that I have tried after that either return me a long string with time and GMT etc, or the same string that I passed to the function.
It sounds like you've already got the parsing part sorted - that's entirely separate from the formatting part.
For formatting, I suspect you want:
DateFormat localeFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
String text = localeFormat.format(convertedDate);
... experiment with SHORT
, MEDIUM
, LONG
and FULL
to see which one meets your needs best, but I suspect it'll be SHORT
or MEDIUM
.
(You can omit the second argument to getDateInstance
and it will use the default locale, but personally I'd advise including it explicitly for clarity.)
See more on this question at Stackoverflow