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.MINUTE, 0);
  calendar.set(Calendar.SECOND, 0);
  calendar.set(Calendar.MILLISECOND, 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?
LVL 9
yongsingAsked:
Who is Participating?
 
riaancorneliusCommented:
0 means midnight, and when you print it out, it's formatted differently :)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
the value you are getting is correct, I believe. It means 12:00 midnight.
0
 
riaancorneliusCommented:
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.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Mayank SAssociate Director - Product EngineeringCommented:
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).
0
 
riaancorneliusCommented:
Try:
 java.util.SimpleDateFormat formatter = new  java.util.SimpleDateFormat("EEE MMM dd HH':'mm'ss' zzz yyyy");
System.out.println( formatter.format(calender.getTime()) );
0
 
yongsingAuthor Commented:
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?
0
 
riaancorneliusCommented:
> System.out.println ( calendar.getTime () )
That's what I meant... Got myself a bit confused there :)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
What is date1 and date2?
0
 
riaancorneliusCommented:
Show us the whole block of code.... The dates you assign has to be different.
0
 
riaancorneliusCommented:
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?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Effectively, the getCalendar () method should anyway make everything to 0, so I guess we need to see the full class here.
0
 
yongsingAuthor Commented:
>> 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".
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
el_diosCommented:
have you ever tried the calendar.clear() method? I think this clears the date
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.