An object, such as java.sql.Dateand java.util.Date(of which it java.sql.Dateis a subclass), does not have its own format. You use an object java.text.DateFormatto display these objects in a specific format, namely DateFormat(rather than itself Date), which defines the format.
For instance:
Date date = ...;
DateFormat df = new SimpleDateFormat("dd MMMM yyyy");
String text = df.format(date);
System.out.println(text);
Note. When you print an object Datewithout using the object DateFormat, for example:
Date date = ...;
System.out.println(date);
then it will be formatted using some default format. However, this default format is not a property of an object Datethat you can change.
source
share