Home java Java, to get the current date in the format type of 12/23/2020...

Java, to get the current date in the format type of 12/23/2020 Date

Author

Date

Category

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

  • format () – takes Date, returns a string
  • .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions