yongsing
asked on
Setting time of Calendar to zeros
I have a method that returns a Calendar with the time set to zeros:
public static Calendar getCalendar(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR , 0);
calendar.set(Calendar.MINU TE, 0);
calendar.set(Calendar.SECO ND, 0);
calendar.set(Calendar.MILL ISECOND, 0);
return calendar;
}
Somewhere in the class I call this method:
Calendar calendar = getCalendar(date);
When I do a print out of calendar.getTime(), I get something like:
Wed Apr 19 12:00:00 SGT 2006
Why is the time at 12:00:00 when I already set it to zeros?
public static Calendar getCalendar(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR
calendar.set(Calendar.MINU
calendar.set(Calendar.SECO
calendar.set(Calendar.MILL
return calendar;
}
Somewhere in the class I call this method:
Calendar calendar = getCalendar(date);
When I do a print out of calendar.getTime(), I get something like:
Wed Apr 19 12:00:00 SGT 2006
Why is the time at 12:00:00 when I already set it to zeros?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
When you print it out, are you just using calender.toString()? It might be usefull to use a java.util.SimpleDateFormat to format it to display a 24 hour version, not 12 Hour. Then you should get the correct result.
I think he's printing it using System.out.println ( calendar.getTime () ) ; that will use the default format which Date class has for printing (its toString () method).
Try:
java.util.SimpleDateFormat formatter = new java.util.SimpleDateFormat ("EEE MMM dd HH':'mm'ss' zzz yyyy");
System.out.println( formatter.format(calender. getTime()) );
java.util.SimpleDateFormat
System.out.println( formatter.format(calender.
ASKER
I have two calls to getCalendar():
Calendar calendar1 = getCalendar(date1);
Calendar calendar2 = getCalendar(date2);
The first one prints "Wed Apr 19 00:00:00 SGT 2006"
The second one prints "Wed Apr 19 12:00:00 SGT 2006"
Why does the first one show 00:00:00 and the second one shows 12:00:00?
Calendar calendar1 = getCalendar(date1);
Calendar calendar2 = getCalendar(date2);
The first one prints "Wed Apr 19 00:00:00 SGT 2006"
The second one prints "Wed Apr 19 12:00:00 SGT 2006"
Why does the first one show 00:00:00 and the second one shows 12:00:00?
> System.out.println ( calendar.getTime () )
That's what I meant... Got myself a bit confused there :)
That's what I meant... Got myself a bit confused there :)
What is date1 and date2?
Show us the whole block of code.... The dates you assign has to be different.
also try printing them using the code I gave earlier (oops, there was a small typo... fixed here):
java.util.SimpleDateFormat formatter = new java.util.SimpleDateFormat ("EEE MMM dd HH':'mm':'ss zzz yyyy");
System.out.println( formatter.format(calender. getTime()) );
What does this output?
java.util.SimpleDateFormat
System.out.println( formatter.format(calender.
What does this output?
Effectively, the getCalendar () method should anyway make everything to 0, so I guess we need to see the full class here.
ASKER
>> Show us the whole block of code.... The dates you assign has to be different.
Does it matter if the date is same or not? In any case, I have set the time fields to zeros. So both calls should show the same time, but it's not.
Anyway, I changed
calendar.set(Calendar.HOUR , 0);
to
calendar.set(Calendar.HOUR _OF_DAY, 0);
and now both calls will show "Wed Apr 19 00:00:00 SGT 2006".
Does it matter if the date is same or not? In any case, I have set the time fields to zeros. So both calls should show the same time, but it's not.
Anyway, I changed
calendar.set(Calendar.HOUR
to
calendar.set(Calendar.HOUR
and now both calls will show "Wed Apr 19 00:00:00 SGT 2006".
Java Doc says:
HOUR: "Field number for get and set indicating the hour of the morning or afternoon."
HOUR_OF_DAY: "Field number for get and set indicating the hour of the day."
So if you use HOUR, the value you have considers if its morning or afternoon, hence it sets it to 12 if you assign it 0.
If you use HOUR_OF_DAY, it assigns it to 0 because then you are considering a 0 to 24 hour day.
HOUR: "Field number for get and set indicating the hour of the morning or afternoon."
HOUR_OF_DAY: "Field number for get and set indicating the hour of the day."
So if you use HOUR, the value you have considers if its morning or afternoon, hence it sets it to 12 if you assign it 0.
If you use HOUR_OF_DAY, it assigns it to 0 because then you are considering a 0 to 24 hour day.
have you ever tried the calendar.clear() method? I think this clears the date