java.util.Date
objects do not have a format by themselves. They are just timestamp values, just like an int
is just a number, without any inherent format. So, there is no such thing as "a Date
object with the format yyyy-MM-dd
".
The format is determined at the moment you convert the Date
to a String
. You can use SimpleDateFormat
to convert a Date
to a String
in a specific format. For example:
Date date = new Date();
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
String text = fmt.format(date);
System.out.println(text);