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



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

.



Relevant Pages

  • Re: Tasks duration (errors?)
    ... faith in you Steve). ... time a worker spends actively engaged in the task and calendar elapsed time ... Visit http://www.mvps.org/project/faqs.htm for the FAQs ... task's duration as "4 months" Project says that it is 80 working days. ...
    (microsoft.public.project)
  • Re: Change Hours in a Work Week
    ... I need to change specific weeks for specific resources for specific hours. ... Jim Aksel, MVP ... "Steve" wrote: ... Whatever the last entry is becomes the default for the entire calendar for the resource. ...
    (microsoft.public.project)
  • Re: Task Constraints
    ... This is exactly the situation you interpret, Steve. ... > you can't "Always Start Before 9am" by using constraints. ... > is to create a calendar that show hours of work from, say, 07:59 to 08:59. ... > between the task you need to start before 9 and its predecessor. ...
    (microsoft.public.project)
  • Re: Calendars (how to add weekends) ?
    ... Steve thanks for the input, Our group is really not using "Project" to its' ... > calendar of interest click the Sat and Sun column heading. ... > takes 15 days of work to complete, the resource doing it isn't going to have ... I doubt that is an accurate description of reality, ...
    (microsoft.public.project)
  • Re: A qiuestion for the community
    ... "Steve, what do you say we put an end to this nonsense? ... Need a month calendar or 7 day calendar? ... Need appointment scheduling? ... >> killfile those and never see them. ...
    (comp.databases.ms-access)