Reset time part of Calendar instance in Java

sergionni picture sergionni · Dec 6, 2010 · Viewed 18.4k times · Source

There is Java code,which is ran on 2 different environments and inserts records with JdbcTemplate to DB.

Results of its running are different for both envs. Particularly,for Date fields.

On first environment(Oracle XE) it produces record:

"12/03/2010";191094;"71697211000";3229;880323202;NULL;0;1;0;NULL;0;NULL

Second environment(Oracle non XE):

"12/03/2010 12:00:00";191094;"71697211000";3229;880323202;NULL;0;1;0;NULL;0;NULL

NLS_DATE_FORMAT(if it's crucial) for first env is DD-MON-RR, for second env is DD-MON-RRRR

The question is,what Oracle settings may affect,that second env Date format is another?

Answer

sergionni picture sergionni · Dec 9, 2010

should set following Calendar properties in Java code:

  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
  cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
  cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
  cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));

instead of:

  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.HOUR_OF_DAY, 0);
  cal.set(Calendar.MINUTE, 0);
  cal.set(Calendar.SECOND, 0);
  cal.set(Calendar.MILLISECOND, 0);