Re: JAVA Time Problem: Want to get the current time and perform validation
- From: "The One" <ankitdave@xxxxxxxxx>
- Date: 28 Nov 2006 15:29:57 -0800
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
Steve W. Jackson wrote:
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
.
- References:
- JAVA Time Problem: Want to get the current time and perform validation
- From: The One
- Re: JAVA Time Problem: Want to get the current time and perform validation
- From: Steve W. Jackson
- JAVA Time Problem: Want to get the current time and perform validation
- Prev by Date: Re: about jacob and jdk's corba implmentation
- Next by Date: Re: JAVA Time Problem: Want to get the current time and perform validation
- Previous by thread: Re: JAVA Time Problem: Want to get the current time and perform validation
- Next by thread: Removing a jsessionid for a soon to go invalid session
- Index(es):
Relevant Pages
|