Date formatting with Calendar dd/MM/yyyy

Asked

Viewed 15,301 times

6

I’m having trouble formatting the date to 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’s how I show it on the screen

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

As I’m setting in my Dao

 Calendar dataI = Calendar.getInstance();
                dataI.setTime(rs.getDate("dataInicio"));
                viagem.setDataInicio(dataI);

2 answers

8


The toString() of Date uses the format dow mon dd hh:mm:ss zzz yyyy to return as a String the value of the date, where:

  • dow is the day of the week, in your case Sat(saturday)
  • mon is the month, the Feb(February) of its result
  • dd is the day of the month represented as two digits, of 01 to 31, in your case day 26
  • hh is the time of day, with values of 00 until 23, the 11 of your leaving.
  • mm is the representation, also in two digits, of the minute, the 38 of your leaving
  • ss is the second, again represented in two digits, of 00 until 61
  • zzz is the time zone, the BRT may be empty if not available
  • yyyy is the year, in four digits, as the 2015

So for the output you need, just use a DateFormat in the pattern you need, in case, dd-MM-yyyy. In this pattern, we will have:

  • dd the day of the month, ranging from 01 to 31
  • MM the month, no longer represented as Jan, Feb, etc., but in two digits, of 01 to 12
  • yyyy being the year, in four digits

An example using Calendar 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 in the pattern you need.

Another point that is perhaps important to note: since getDate(String) returns a java.sql.Date and she is the subclass of java.util.Date, it is not necessary to configure it in a Calendar, you can do directly, thus:

final DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
final Date date = rs.getDate("dataInicio");
System.out.println(df.format(date));

To display correctly on your dialog, you can use something this way:

final DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
final String dataFormatada = df.format(contato.getDataInicio().getTime());
JOptionPane.showMessageDialog(null, "Data de inicio: " + dataFormatada);

5

When you perform

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

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

You can still use the class Simpledateformat to format your date output 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_);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.