• Status: Solved
  • Priority: High
  • Security: Private
  • Views: 47
  • Last Modified:

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
0
armbsu
Asked:
armbsu
  • 5
  • 3
  • 3
  • +3
1 Solution
 
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
 
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 5
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now