2

Why does this keep returning 0?

Calendar calendar = Calendar.getInstance();
calendar.get(Calendar.HOUR_OF_DAY)?
2
  • By any chance is it between 12:00am and 12:59am in your time zone? Commented May 18, 2011 at 1:03
  • The value of HOUR_OF_DAY is from 0-24. Perhaps when you run that code, the equivalent time is 0. Did you try running it per hour? Commented May 18, 2011 at 1:09

2 Answers 2

3

Calendar.HOUR_OF_DAY will be 0 whenever the hour of the day falls between between midnight and one o'clock in the morning. So chances are, either it is in that time interval wherever you are, or your Android emulator is set up so it thinks you're in that time interval.

If that's not the case, you could try setting the hour to some arbitrary value and then getting it again to check that it's working. To do that, try:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 5);
Sign up to request clarification or add additional context in comments.

Comments

0

Ideally it should not,

Try this

Calendar calendar = Calendar.getInstance();
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.HOUR));
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
System.out.println(calendar.get(Calendar.MINUTE));

Nothing different from what you have except I am trying to print year,hour, hour of day,minute.

1 Comment

hrmm... maybe it's my android emulator? But this seems to work fine: calendar.get(Calendar.DAY_OF_WEEK);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.