?
Solved

Setting time of Calendar to zeros

Posted on 2006-04-19
16
Medium Priority
?
279 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:yongsing
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 9

Accepted Solution

by:
riaancornelius earned 100 total points
ID: 16486369
0 means midnight, and when you print it out, it's formatted differently :)
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 100 total points
ID: 16486378
the value you are getting is correct, I believe. It means 12:00 midnight.
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 16486382
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 16486410
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
 
LVL 9

Expert Comment

by:riaancornelius
ID: 16486411
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
 
LVL 9

Author Comment

by:yongsing
ID: 16486418
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
 
LVL 9

Expert Comment

by:riaancornelius
ID: 16486422
> System.out.println ( calendar.getTime () )
That's what I meant... Got myself a bit confused there :)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16486427
What is date1 and date2?
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 16486448
Show us the whole block of code.... The dates you assign has to be different.
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 16486465
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16486472
Effectively, the getCalendar () method should anyway make everything to 0, so I guess we need to see the full class here.
0
 
LVL 9

Author Comment

by:yongsing
ID: 16486611
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16486664
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
 
LVL 3

Expert Comment

by:el_dios
ID: 16488569
have you ever tried the calendar.clear() method? I think this clears the date
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question