Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1570
  • Last Modified:

Calendar.HOUR_OF_DAY shows wrong timing

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
FranklinRaj22
Asked:
FranklinRaj22
  • 5
  • 4
  • 3
  • +3
1 Solution
 
HainKurtSr. System AnalystCommented:
you should use getInstance first

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

I guess ;)
0
 
FranklinRaj22Author Commented:
i assign a gregorian calendar instance before tryin to pull the hour of the day information.

Calendar calendar = new GregorianCalendar();
0
 
HainKurtSr. System AnalystCommented:
then use

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

not

Calendar.HOUR_OF_DAY
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CPColinSenior Java ArchitectCommented:
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
 
HainKurtSr. System AnalystCommented:
0
 
HainKurtSr. System AnalystCommented:
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
 
objectsCommented:
you can set the timezonme using the setTimezone() method
0
 
CEHJCommented:
>>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
 
FranklinRaj22Author Commented:
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
 
FranklinRaj22Author Commented:
Thanks Guys , I did this and it wrked ....

while(calendar.get(Calendar.HOUR_OF_DAY) < 20){
0
 
objectsCommented:
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
 
CEHJCommented:
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
 
CEHJCommented:
>>You'll be eating a lot of CPU cycles like that.

(and unnecessary allocations)
0
 
FranklinRaj22Author Commented:
let me use call back and see ..
0
 
mrcoffee365Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now