Recently I made an edit to this question because the previous edit fixed a bug in the question's code, which resulted in the question's output not matching what the code actually outputted anymore. This edit was rejected, but I feel like it should not have been.
Originally, the question's code looked like this:
String date_s = " 2011-01-18 00:00:00.0";
SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss");
Date date = dt.parse(date_s);
SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd");
System.out.println(dt1.format(date));
Following the code, the question says:
But it outputs: 02011-00-1 instead of the desired 2011-01-18.
Notice how there are five y
's, and also notice that the five y
's results in five digits in the outputted year: 02011
.
The previous edit changed the code so that there were only four y
's. Here's the new, "corrected" code from that edit:
String date_s = " 2011-01-18 00:00:00.0";
SimpleDateFormat dt = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date date = dt.parse(date_s);
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-mm-dd");
System.out.println(dt1.format(date));
The problem is that when running this code, the output is now 2011-00-18
, not 02011-00-1
as stated in the question.
My edit added the fifth y
back into the question, where it should stay, because not only does the question's output depend on it, but so do many answers and even the highest rated comment.
Some examples of answers that mention the five y
's include:
https://stackoverflow.com/a/4772461/2364405
Also, years exist of four digits, not five.
https://stackoverflow.com/a/4772431/2364405
Also, yyyyy is not the same as yyyy
https://stackoverflow.com/a/18953213/2364405
remove one y form SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd");
So, why was my edit rejected? Clearly, it makes more sense to leave the five y
's in than out.
@Maksim ...
), so you could try getting the previous editor to roll back.