Reset time part of Calendar instance in Java
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_F开发者_如何学编程ORMAT(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?
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);
According to the documentation, HH
refers to a 12-hour time. The 12 in the time you're retrieving is 12 midnight. What you want is HH24
, which ges you a 24-hour time, starting at 00 for midnight.
If you don't want to show the time part, don't include a format string which includes the time part ("HH:MI:SS").
You've reset the time part to midnight, basically... there's no way of differentiating between a Calendar
or Date
set to exactly midnight and a Calendar
or Date
"without" a time part - because there's no such concept as a Calendar
/Date
with only a date part.
Now you may be able to have that in the database, depending on what types are available to you - but java.util.Date
and java.util.Calendar
always represent points in time, not just dates.
The reason it's showing 12 instead of 00 is because you're using "HH" instead of "HH24", as per lacqui's answer. I assume you don't really want to see the time at all though, given that you'll have reset it to midnight...
I'd recommend you extend one of the Calendar classes like this:
public class CalendarDateOnly extends GregorianCalendar {
public static Calendar getInstance() {
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));
return cal;
}
}
Instantiate in this way:
Calendar june30 = CalendarDateOnly.getInstance();
june30.set(2000, Calendar.JUNE, 30);
/*
* Date : 2015-07-09
* Author : Bhuwan Prasad Upadhyay
*/
package com.developerbhuwan.date.utils;
import java.util.Calendar;
import java.util.Date;
/**
*
* @author developerbhuwan
*/
public class CalenderUtils {
public static Calendar getNewCalendarInstance() {
Calendar calendar = Calendar.getInstance();
return resetCalender(calendar);
}
public static Date resetDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return resetCalender(calendar).getTime();
}
public static Calendar resetCalender(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar;
}
public static void setTimeInCalendar(Calendar calendar, Date date) {
calendar.setTime(resetDate(date));
}
}
精彩评论