I cannot format date

Asked

Viewed 184 times

4

I’m trying to format one date to look like another, the first comes straight from the bank but the other needs to be in the same format.

Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String d = df.format(cal.getTime());

Date antigo = rs.getDate("datapi");
DiasUteis du = new DiasUteis();

Date data = new SimpleDateFormat("yyyy-MM-dd").parse(d);

System.out.println(antigo + " - " + data);

The result is as follows:

2017-10-03 - Mon Jul 02 00:00:00 BRT 2018

2 answers

3

Supposing rs is a java.sql.ResultSet


First, an explanation of what happens.

The method rs.getDate() returns a java.sql.Date, and the code only works because this is a subclass of java.util.Date. But the object that is there in the variable antigo is a java.sql.Date.

Already the method SimpleDateFormat.parse() returns a java.util.Date.

When you print both with System.out.println, internally it calls the method toString() of these classes. And in each of them this method returns the date in a different format:

  • in java.sql.Date, the format is used ISO 8601 (2017-10-03)
  • in java.util.Date, is used this other format (which I can’t remember if it has a specific name): Mon Jul 02 00:00:00 BRT 2018

But that doesn’t mean that the date is in this format. In fact, as already said here, here and here, dates have no format.

Probably the field in the database is of some kind of date (like DATE, DATETIME, TIMESTAMP, etc., depends on the bank), and internally only values are recorded (usually the numerical values of the date, or any other internal representation, no matter).

What the method toString() does is convert these values to a text (a String) in some specific format, but that does not mean that the date is in that format.

If I want a date (values number of day, month, year, etc.), I use the respective objects (Date, Calendar, etc.).

If I want the date values in a specific format, I Gero a String using specific classes for this purpose, for example SimpleDateFormat.


Now let’s go to your code:

Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String d = df.format(cal.getTime());

Here you created a String (the variable d), which contains the current date (returned by Calendar.getInstance()), in the "year-month-day" format. Why didn’t you just print this date, since it is in the format you need? (unless this is not the date you want to print).


Date antigo = rs.getDate("datapi");

Here you are getting the java.sql.Date. If you want this date in the correct format, just call antigo.toString(), that returns a String in the "year-month-day format".


Date data = new SimpleDateFormat("yyyy-MM-dd").parse(d);

And here you are turning the String d in a java.util.Date. This is unnecessary, because the d already contains a String with the date in the format you want (and even if needed, you could use the variable df created earlier, rather than creating another SimpleDateFormat).

Anyway, if you have an object Date and wants to generate a String in a specific format, use SimpleDateFormat and the method format, passing the Date as a parameter.

2

Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String d = df.format(cal.getTime());

Date antigo = rs.getDate("datapi");
DiasUteis du = new DiasUteis();

Date data = new SimpleDateFormat("yyyy-MM-dd").parse(d);

System.out.println(antigo + " - " + df.format(data));

You could test the above code and comment on the result?

Browser other questions tagged

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