I can not format date

4

I'm trying to format one date to be the same as 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

    
asked by anonymous 03.07.2018 / 00:00

2 answers

3

Assuming that rs is java.sql.ResultSet

First, an explanation of what happens.

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 SimpleDateFormat.parse() returns java.util.Date .

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

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

But that does not mean that the date is in this format. Probably the field in the database is of some kind of date (such as DATE, DATETIME, TIMESTAMP, etc, depends on the database), and internally only values are recorded (usually the numeric values of the date, or any other internal representation, does not matter ).

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

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

If I want date values in a specific format, I generate a String using specific classes for this purpose, such as 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 have created a String (the variable d ), which contains the current date (returned by getInstance() ), in the "year-month-day" format. Why did not you simply 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 java.sql.Date . If you want this date in the correct format, just call antigo.toString() , which returns String in the "year-month-day" format.

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

And here you are transforming String d into java.util.Date . This is unnecessary because d already contains a String with the date in the format you want (and even if necessary, you could use the df variable previously created, instead of creating another SimpleDateFormat ) .

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

    
03.07.2018 / 00:32
1
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));

Could you test the code above and comment on the result?

    
03.07.2018 / 00:19