-2

I am doing some junit test. I am getting the date as response in the form of date as below :

2017-08-14 00:00:00.0 +0:00

The data which is present in oracle DB is

14-AUG-17 12.00.00.000000000 AM +00:00

I want to use an assert like this but it is failing. can anyone help to make sure that both expected and actual matches.

Assert.assertEquals("14-08-2017", 2017-08-14 00:00:00.0 +0:00); 
user2326831
  • 151
  • 2
  • 14
  • In Oracle, a date does not have a format [it is stored as 7- or 8-bytes](https://stackoverflow.com/documentation/oracle/2087/dates/6848/the-format-of-a-date). If a format is applied to a date it is done by the user interface accessing the database. In the case of SQL/Plus or SQL developer, the format is determined by the `NLS_DATE_FORMAT` session parameter; other interfaces (e.g. Java) may apply different default formatting or may just store it in its original [byte format](https://docs.oracle.com/cd/E16338_01/appdev.112/e13995/oracle/sql/TIMESTAMP.html). – MT0 Aug 09 '17 at 11:10

2 Answers2

1

You can use SimpleDateFormat to make string from date.

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Assert.assertEquals("14-08-2017", sdf.format(<your date instance>));
Vlad Bochenin
  • 3,007
  • 1
  • 20
  • 33
1

You can create two Date objects and for the assertion.

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
try {
    Date parsedDate1 = formatter.parse("14-08-2017");
    System.out.println(parsedDate1);

    SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
    Date parsedDate2 = formatter2.parse("2017-08-1 00:00:00.0 +0:00");
    Assert.assertEquals(parsedDate1, parsedDate2);

} catch (ParseException e1) {
}
QuakeCore
  • 1,886
  • 2
  • 15
  • 33