java date is coming as 18 instead of 2018

I have my code as follows-

DateFormat source = new SimpleDateFormat("MM/dd/yy");
DateFormat target = new SimpleDateFormat("MM-dd-yyyy");
Date date = source.parse("1/1/2018");
String formattedDate = target.format(date); 

Open in new window


however, this gives me output as 1-JAN-18,  I want this to be 1-JAN-2018.  Thank you
armbsuAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Jan LouwerensSoftware EngineerCommented:
Your source format is only specifying 2 digits for the year, yet you're passing 4 digits. It's allowing this because the Lenient flag is still set on the DataFormat.

Try this:
DateFormat source = new SimpleDateFormat("MM/dd/yyyy");
DateFormat target = new SimpleDateFormat("MM-dd-yyyy");
Date date = source.parse("1/1/2018");
String formattedDate = target.format(date); 

Open in new window

1

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
Jan LouwerensSoftware EngineerCommented:
Also, I just noticed that your DateFormats specify the month first, but your explanation specifies the day-of-month first.
0
Nitin SontakkeDeveloperCommented:
@Jan, could it be because values are interchangeable in this particular case. I have no knowledge of Java.
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Can you please post your actual code?

There is no way that the code above would give you the output that you say you are getting.
0
Jan LouwerensSoftware EngineerCommented:
Try this:

DateFormat source = new SimpleDateFormat("dd/MM/yyyy");
DateFormat target = new SimpleDateFormat("d-MMM-yyyy");
Date date = source.parse("1/1/2018");
String formattedDate = target.format(date).toUpperCase();

Open in new window

0
A mCommented:
@Jan Louwerens , thanks that worked.
0
CEHJCommented:
armbsu, please don't waste people's time here by posting code that is not the real code.
0
A mCommented:
CEHJ,   Here are some points that I want to say-
1. I am just starting out in Java. I struggled for this Date problem for nearly 3 hours, and then posted this question.
2.  I am a paid member of this site, and I too don't want waste my money and time to report something that it is not true
3.  I tried the solution posted by Jan and it worked

Thank you
0
CEHJCommented:
Unfortunately none of those points is relevant. But, taking up the third, you were lucky - Jan ignored your (false) first comment and took the time to figure out what was really happening. You might not always be so lucky
0
A mCommented:
that means it was real code. I was not able to figure out what was really happening.  I might not be lucky next time that's why I have this forum.
0
CEHJCommented:
Do you really not understand that the code you posted does not correspond to the comment you posted about it?
0
armbsuAuthor Commented:
It was the real code, believe me. If not, please don't waste your time discussing - which was already solved  , instead please help someone who needs your expertise.
0
CEHJCommented:
OK. I had to guess (which i shouldn't have to) More often the code is false. In your case it was the comment that was false
0
armbsuAuthor Commented:
Wrong - My code and comments were both real and true
0
CEHJCommented:
Then perhaps you can explain how any of the posted code could possibly produce:
however, this gives me output as 1-JAN-18
in simple words?
0
armbsuAuthor Commented:
IDK, that's why I posted this question here.  There was a simple mistake in code and that was pointed out. Sometimes you need expert eyes to look into your code.
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.