Solved

Calendar.HOUR_OF_DAY shows wrong timing

Posted on 2010-09-23
17
1,453 Views
Last Modified: 2012-05-10
I am trying to do some time calculation using the Calendar.HOUR_OF_DAY but it always gives the output as 11 . I tried to change the timezone in my PC to different time zone and still no difference . Where does this Java function look for the current time ? will it look in the machine in whicjh we are running the program for the current time?
0
Comment
Question by:FranklinRaj22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +3
17 Comments
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33748041
you should use getInstance first

Calendar rightNow = Calendar.getInstance();
then use rightNow.HOUR_OF_DAY

I guess ;)
0
 

Author Comment

by:FranklinRaj22
ID: 33748270
i assign a gregorian calendar instance before tryin to pull the hour of the day information.

Calendar calendar = new GregorianCalendar();
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33748400
then use

Calendar calendar = new GregorianCalendar();
alert(calendar.HOUR_OF_DAY);

not

Calendar.HOUR_OF_DAY
0
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!

 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 33748445
Calendar.HOUR_OF_DAY is the constant you pass to Calendar.get() to get the right value. You want something like:

calendar.get(Calendar.HOUR_OF_DAY);
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33748573
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33748591
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33748596
sample
// create a GregorianCalendar with the Pacific Daylight time zone
 // and the current date and time
 Calendar calendar = new GregorianCalendar(pdt);
 Date trialTime = new Date();
 calendar.setTime(trialTime);

 // print out a bunch of interesting things
 System.out.println("ERA: " + calendar.get(Calendar.ERA));
 System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
 System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
 System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
 System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 33750201
you can set the timezonme using the setTimezone() method
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33752447
>>I am trying to do some time calculation using the Calendar.HOUR_OF_DAY but it always gives the output as 11

Please post your code
0
 

Author Comment

by:FranklinRaj22
ID: 33756883
Piece of my code : Please advice.

public void run()
    {
       Thread t = Thread.currentThread();
        String name = t.getName();
        System.out.println("entered loop() - " + name);
        Calendar calendar = new GregorianCalendar();        
//int hour = Calendar.get(calendar.HOUR_OF_DAY);
        while(calendar.HOUR_OF_DAY < 20){  
         S.O.P("time is not 8 PM yet")
        }
   }
0
 

Author Comment

by:FranklinRaj22
ID: 33756917
Thanks Guys , I did this and it wrked ....

while(calendar.get(Calendar.HOUR_OF_DAY) < 20){
0
 
LVL 92

Expert Comment

by:objects
ID: 33757934
that should actually be:

public void run()
{
       Thread t = Thread.currentThread();
        String name = t.getName();
        System.out.println("entered loop() - " + name);
        while(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) < 20){  
            S.O.P("time is not 8 PM yet")
        }
   }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33758075
You'll be eating a lot of CPU cycles like that. Use a timer with a 1 second cycle and a callback if required
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33758094
>>You'll be eating a lot of CPU cycles like that.

(and unnecessary allocations)
0
 

Author Comment

by:FranklinRaj22
ID: 33939690
let me use call back and see ..
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 37419067
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
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

Suggested Solutions

Title # Comments Views Activity
How  can  i  resolve  HTTP Status 404 -? 8 79
sql import cannot be resolved jsp 3 117
Java basic valueOf question 1 50
Html split(text) 2 49
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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

738 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