Get Today’s Date



This code example shows how to output today’s date. It uses the Calendar class. Since it is an abstract class and therefore cannot be instantiated we created it using the getInstance() method. Then we create an instance of the SimpleDateFormat class which takes the format we want to display the date in as argument to the constructor.
Finally the method ‘format()’ is called on the SimpleDateFormat object. The format() method takes a java.util.Date object as argument and we can get our hands on one by calling the getTime() method of the Calendar instance.

The output from the code above prints the current date.

UPDATE

From Java 8 there’s a new date API. To get the current date in different formats with this new API check out the following link:

Display date in different formats with the LocalDate class







Search for more Java info on this site here:
Custom Search


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">