Returns – Wed Dec 23 15:02:32 IST 2020 , if necessary – 2020-12-23 .
Thank you.
Thank you all.
The issue is closed.
Thank you again (just less than 30 characters will not accept :)).
Answer 1, Authority 100%
java.util.Date – it is quite difficult arranged object that contains within itself the date and methods of work with them. You probably performed some code like this:
String sDate1 = "2020-12-23";
Date date1 = new SimpleDateFormat ( "yyyy-MM-dd") parse (sDate1).;
System.out.println (date1);
We saw the console Wed Dec 23 15:02:32 IST 2020
and thought that date1
store some wrong line.
But date1
string is not stored. There is stored data (ie, arranged quite difficult object, etc.).
You are very cleverly made the date string – it’s good. And now look how you turn the date to a string the desired species. For example, you can:
SimpleDateFormat formatter = new SimpleDateFormat ( "yyyy-MM-dd");
String format = formatter.format (date1);
System.out.println (format);
With all the same SimpleDateFormat
, by the way.
Just when did you get a line – it is always the same line. And when you have it date – you can use the formatter to choose how it should look like a string with a date
.
Answer 2, Authority 50%
You have everything backwards in the code.
Method parse ()
in the SimpleDateFormat converts the string to a date. Of course, if you print to the console received a copy of Date, the output will be Wed Dec 23 15:02:32 IST 2020
, because it will indicate the resulting Date.
And you’ll probably have to reverse the action. Date to convert to a string. To do this SimpleDateFormat is a method format ()
, which is served in an instance of Date, and goes nicely formatted string.
Once again:
parse ()
– takes a string, returns the Dateformat ()
– takes Date, returns a string
.
.