JAVA Time Problem: Want to get the current time and perform validation

T

The One

Hi,

I am working on a module which has to check the current time. If the
time is within the bounds then the system performs further steps.

I have to use the Eastern std. Time as the base time for the
validation.

Here is the sample code:

TimeZone myTimeZone = TimeZone.getTimeZone("EST");
Calendar calendar = Calendar.getInstance();

int iHour = calendar.get(Calendar.HOUR_OF_DAY);
System.out.print(iHour);

//Time between 8 AM and 8PM EST

if (iHour > 8 & iHour < 20)
{
Go Ahead
}


This code doesn't seem to work correctly.

Can anybody help me out ?

Thanks
Dave
 
D

Daniel Pitts

The said:
Here is the sample code: [snip]
if (iHour > 8 & iHour < 20) [snip]
This code doesn't seem to work correctly.

Usually, its a good idea to say what you expect the code to do, and
what you see it actually doing.
Although, I think I see your problem:

Perhaps you should try "iHour >= 8 && iHour <= 20".
 
S

Steve W. Jackson

"The One said:
Hi,

I am working on a module which has to check the current time. If the
time is within the bounds then the system performs further steps.

I have to use the Eastern std. Time as the base time for the
validation.

Here is the sample code:

TimeZone myTimeZone = TimeZone.getTimeZone("EST");
Calendar calendar = Calendar.getInstance();

int iHour = calendar.get(Calendar.HOUR_OF_DAY);
System.out.print(iHour);

//Time between 8 AM and 8PM EST

if (iHour > 8 & iHour < 20)
{
Go Ahead
}


This code doesn't seem to work correctly.

Can anybody help me out ?

Thanks
Dave

Using a Calendar can be a royal pain...

Your mistake above is that you use the wrong getInstance method on
Calendar. You should use getInstance(TimeZone) and pass it the TZ
you've created just before it. Once you've done that, the Calendar
object you have will reflect values in that time zone and your current
Locale.

You might be better off, however, if you use GregorianCalendar instead.
It has constructors that include one taking a TimeZone. It represents
the calendar used in most of the world.

Also, you've got a logic error. The checks for iHour will only work if
the hour is between 9 and 19, inclusive. If you want it to work after 8
am and before 8 pm, you might need to include checks for minutes and/or
seconds. But if you want the simplest solution that will correctly work
between 8 am and 8 pm inclusive, change your conditions to "iHour >= 8"
and "iHour <= 20" instead.

= Steve =
 
T

The One

Hi Steve,

That was really useful. Just came to my mind what if the clock changes
on the day-time saving period. Does Java Calendar object takes care of
it or I have to manually put validation for that piece of code.

Thanks,
Ankit Dave
 
T

The One

Thanks Daniel.

I should have more careful on it.

ankit


Daniel said:
The said:
Here is the sample code: [snip]
if (iHour > 8 & iHour < 20) [snip]
This code doesn't seem to work correctly.

Usually, its a good idea to say what you expect the code to do, and
what you see it actually doing.
Although, I think I see your problem:

Perhaps you should try "iHour >= 8 && iHour <= 20".
 
S

Steve W. Jackson

[ snip ]
Hi Steve,

That was really useful. Just came to my mind what if the clock changes
on the day-time saving period. Does Java Calendar object takes care of
it or I have to manually put validation for that piece of code.

Thanks,
Ankit Dave

[ top-posting corrected ]

You would be well-served to look at the API Javadocs. You'll learn a
great deal about how Calendar (and thus GregorianCalendar)and TimeZone
work. It's actually the TimeZone that determines whether and when
Daylight Saving Time takes effect. In your original example, you said
you needed to use EST -- Eastern Standard Time. Look into the API docs
and you'll find that the use of 3-letter codes is actually deprecated,
according to the comments in the TimeZone class. Instead, you will want
to identify an appropriate identifier for your use that supports (or
not) DST, as you need. Then the TimeZone will maintain the rules for
whether and when time changes occur, and your Calendar or
GregorianCalendar will respect those rules.

One point of interest: in your original inquiry, you stated that you
needed some action to occur between 0800 and 2000. In any US time zone,
the clock change for DST will occur outside those hours. So the only
point where you would care is if your actions need to be aware of
whether or not DST is in effect.

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,087
Members
48,773
Latest member
Kaybee

Latest Threads

Top