Re: JAVA Time Problem: Want to get the current time and perform validation
- From: "Steve W. Jackson" <stevewjackson@xxxxxxxxxxx>
- Date: Tue, 28 Nov 2006 16:50:09 -0600
In article <1164753517.131600.29720@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"The One" <ankitdave@xxxxxxxxx> wrote:
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 =
--
Steve W. Jackson
Montgomery, Alabama
.
- Follow-Ups:
- References:
- Prev by Date: Re: How to update a jar file for one source file change?
- Next by Date: Re: How to update a jar file for one source file change?
- Previous by thread: Re: JAVA Time Problem: Want to get the current time and perform validation
- Next by thread: Re: JAVA Time Problem: Want to get the current time and perform validation
- Index(es):
Relevant Pages
|