Formatting dates with calendar dd / MM / yyyy

5

I'm having trouble formatting the date for the dd / MM / yyyy format

 Como aparece:                 Sat Feb 26 11:38:28 BRT 2015

 Como eu quero que apareça:    04-07-2015

Here is how I show on the screen

 JOptionPane.showMessageDialog(null, "Data de inicio: " + contato.getDataInicio().getTime());

How I'm setting up in my Dao

 Calendar dataI = Calendar.getInstance();
                dataI.setTime(rs.getDate("dataInicio"));
                viagem.setDataInicio(dataI);
    
asked by anonymous 04.07.2015 / 16:44

2 answers

8

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);
    
04.07.2015 / 17:30
5

When you execute

JOptionPane.showMessageDialog(null, "Data de inicio: " + contato.getDataInicio().getTime());

A call to the Date.toString () method occurs to show the contents of your variable. By default this is the output of the method.

You can, however, use the SimpleDateFormat class to format the output of your date as follows:

SimpleDateFormat format_ = new SimpleDateFormat("yyyy-MM-dd");
String dataFormatada_ = format_.format(contato.getDataInicio().getTime());
//System.out.println(dataFormatada_);

JOptionPane.showMessageDialog(null, "Data de inicio: " + dataFormatada_);
    
04.07.2015 / 17:27