• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting system time inside while loop  RSS feed

 
Shiny Angel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have problem getting system time inside while loop. The first time it picks the sys time. But in the second iteration, even if the sys time changes, the hour and minute integers are having the same value only. Could somebody help, why the consecutive iterations fail to reassign the values??

Below is my code:

int g = 1;
int hour = 0;
int minute = 0;
GregorianCalendar calendar = new GregorianCalendar ( ) ;
while(true)
{
System.out.println("hr :: "+hour);
System.out.println("min :: "+minute);
System.out.println("in :: "+g);
hour = calendar.get ( GregorianCalendar.HOUR_OF_DAY ) ;
minute = calendar.get ( GregorianCalendar.MINUTE ) ;
System.out.println("::SysTime::"+hour);
System.out.println("::SysTime::"+minute);
if (hour == 17 && minute == 44)
{
System.out.println("Inside if");
t.interrupt();
break;
}
g++;
}

Thanks,
Shiny.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).

here



You could try to set the specific instant in time for the Calendar object by using some of the set methods (eg. setTime(Date), setTimeInMillis(long)) in each iteration of the loop.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should get time in milliseconds since your each for loop iteration doesnt take much time. Its better to use setTimeInMillis(long))..
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should check time in milliseconds, as each iteration of for loop is fast. So use setTimeInMillis(long))
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create a GregorianCalendar object it represents the time at the instant the object was created. It will not change unless you use one of its set methods to explicitly change it.
If you want the current time every time thru the loop then you need to create a new GregorianCalendar instance every time.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Shiny" you have obviously missed the naming policy when you logged on. We require you display first name-space-last name not obviously fictitious. Since your displayed name doesn't match that pattern, please go to "my profile" "update profile" and correct it to comply.

CR
 
Shiny Angel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thank you for your replies. It helped me solving my problem and understand what my mistake was.

Special thanks for Joanne Neal

@ CR

I have updated now with my complete name.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks fake though. Can you PM some bartender or sheriff (Campbell or me will be fine) with something that shows us it really is your name?
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have said I thought the name looked fictional only Rob got there first.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!