Printing Date/Time in a Given Format

Date todaysDate = new java.util.Date();
											SimpleDateFormat formatter =
											new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
											String formattedDate = formatter.format(todaysDate);
											System.out.println("Today's Date and Time is: "
											+ formattedDate);

Java contains formatting classes that can be used to format a date into a desired format. The most commonly used class for formatting dates is the SimpleDateFormat class. This class takes a format string as input to its constructor and returns a format object that can then be used to format Date objects. Calling the format() method of the SimpleDateFormat object will return a string that contains the formatted representation of the Date that is passed into the method as a ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.