toString()
from Date
uses the format dow mon dd hh:mm:ss zzz yyyy
to return as a String
the date value, where:
-
dow
is the day of the week, in your case Sat
(Saturday)
-
mon
is the month, Feb
(February) of your result
-
dd
is the day of the month represented as two digits, from 01 to 31 , in your case day 26
-
hh
is the time of day with values from 00 to 23 , 11 of your output.
-
mm
is the representation, also in two digits, of the minute, the 38 of its output
-
ss
is the second, again represented in double digits, from 00 to 61
-
zzz
is the time zone , the BRT
of its output, it can be empty if it is not available
-
yyyy
is the year, in four digits, like 2015
So, for the output you need, just use a DateFormat
in the pattern you need, in this case, dd-MM-yyyy
. In this pattern, we will have:
-
% by day of the month, ranging from 01 to 31
-
dd
month, no longer represented as MM
, Jan
, etc., but in double digits, from 01 to 12
-
Feb
being the year, in four digits
An example using yyyy
would be this:
final DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
final Calendar cal = Calendar.getInstance();
System.out.println(df.format(cal.getTime()));
This will print to the pattern you need.
Another point that might be important to note is: Calendar
" returns a getDate(String)
and it's subclass of java.sql.Date
, it's not need to set it in a calendar, you can do it directly, like this:
final DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
final Date date = rs.getDate("dataInicio");
System.out.println(df.format(date));
To display correctly in your dialog , you can use something like this:
final DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
final String dataFormatada = df.format(contato.getDataInicio().getTime());
JOptionPane.showMessageDialog(null, "Data de inicio: " + dataFormatada);