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.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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

0 means midnight, and when you print it out, it's formatted differently :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
the value you are getting is correct, I believe. It means 12:00 midnight.
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.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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).
 java.util.SimpleDateFormat formatter = new  java.util.SimpleDateFormat("EEE MMM dd HH':'mm'ss' zzz yyyy");
System.out.println( formatter.format(calender.getTime()) );
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?
> System.out.println ( calendar.getTime () )
That's what I meant... Got myself a bit confused there :)
Mayank SAssociate Director - Product EngineeringCommented:
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?
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.
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);


calendar.set(Calendar.HOUR_OF_DAY, 0);

and now both calls will show "Wed Apr 19 00:00:00 SGT 2006".
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.
have you ever tried the calendar.clear() method? I think this clears the date
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.