Format today's date in YYYYMMDD format

I am trying to format today's date to YYYYMMDD format by:
String today = new SimpleDateFormat("YYYYMMDD").parse(DateFormat.getDateInstance(DateFormat.MEDIUM).format(Calendar.getInstance().getTime())).toString();

However, what I am getting is the following error:
java.text.ParseException: Unparseable date: "Sep 28, 2015"

I am needing 20150928. Anyone know how to accomplish this?

Thanks
atxmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CPColinSenior Java ArchitectCommented:
Your format string is a little off. Check the Javadocs and try again. Your exception is happening because you're calling the parse() method. You don't need that, since you're formatting the date, not parsing it. Take it out and fix the format string and things should work better.
0
Jim CakalicSenior Developer/ArchitectCommented:
If you breakdown the code, what you are asking is to get the current time
Calendar.getInstance().getTime()
format it to a String using the MEDIUM  (Mon DD, YYYY) date format
DateFormat.getDateInstance(DateFormat.MEDIUM).format
parse that to a Date instance using your target format (YYYYMMDD) <-- where the ParseException happens because the String from the previous step doesn't match your target format
new SimpleDateFormat("YYYYMMDD").parse
and then get the toString of that Date instance

Assuming this starts out with a Date object (Calendar.getInstance().getTime()) all that you really need is to format it:
Date now = Calendar.getInstance().getTime();
SimpleDateFormat target = new SimpleDateFormat("YYYYMMDD");
String today = target.format(now);
What I think you really want for format is YYYYMMdd. The DD format will give you the day number of the year. Today (10/6/2015) for example is day number 279 so you'll get 201510279 when I think you're probably expecting 20151006?

Regards,
Jim
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
atxmanAuthor Commented:
thanks for the insight. very helpful
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.