I format the string in the date, but the date is still displayed in the format Fri Feb 05 00:00:00 MSK 2016 and I want it in the format yyyy-MM-dd

 String s = "2016-02-05"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date m = null; try { m = sdf.parse(s); } catch (ParseException e) { e.printStackTrace(); } _log.info("Дата: " + m); 

What am I doing wrong?

  • where is "output" in _log.info("Дата: " + m); ? - here you do not specify any format. NB You do not “format” a string into a date, but convert or convert. - Igor
  • and how should I specify it? - Vorobey.A
  • I do not know, for example: _log.info(m.toString(sdf)); - Igor

2 answers 2

You do not exactly what you want.

It will be correct like this:

 _log.info("Дата: " + sdf.format(m)); 

That is, you should get a textual representation of your object in the specified format.

  • Hmm, yes, and if I need the date type to be exactly the format that I want? Those. Yes, output through format () is obtained. But I would like the variable to be in this format - Vorobey.A
  • "in date type" there is no format; she does not know, and should not know, how you will show it to the user. - Igor
  • Thank you) then I will think how to do further) But the answer to this question is still true) - Vorobey.A
 Fri Feb 05 00:00:00 MSK 2016 

Which means that the date has been successfully parsed and you are doing everything right.

You print the date , not the string. Dates, like numbers, are not strings.