Calculating
The Business Hours
Question:
I need to calculate business hours i.e, from 0900
- 1700 for all weekdays. I am having a timer which starts on Sunday night
and ends by Saturday morning. I need to exclude all non business hours
and exclude weekends and calculate the working hours alone. The timer may
start any time like on wednesday noon and end by friday noon also. I only
want the business hours taken.
Solution:
You'll need a definition on what the business hours are.
For instance:
Code:
public class BusinessHours
{
private Entry[] entries = new Entry[Calendar.SATURDAY
+ 1];
// initialize entries
public boolean contains(Calendar calendar)
{
int weekDay
= calendar.get(Calendar.DAY_OF_WEEK);
Entry entry
= entries[weekDay];
if (entry
!= null)
{
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int time = hour * 100 + minute;
return time >= entry.start && time <= entry.end;
}
return false;
}
public static class Entry
{
private int
weekDay;
private int
start; // minutes in military style, e.g. 0930
private int
end; // likewise
// constructors,
getters, setters, ...
}
}
public class BusinessHours
{
private Entry[] entries = new Entry[Calendar.SATURDAY
+ 1];
// initialize entries
public boolean contains(Calendar calendar)
{
int weekDay
= calendar.get(Calendar.DAY_OF_WEEK);
Entry entry
= entries[weekDay];
if (entry
!= null)
{
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int time = hour * 100 + minute;
return time >= entry.start && time <= entry.end;
}
return false;
}
public static class Entry
{
private int
weekDay;
private int
start; // minutes in military style, e.g. 0930
private int
end; // likewise
// constructors,
getters, setters, ...
}
}
This code only allows one entry per day, but you could
use List<Entry>[] instead of Entry[] to allow multiple entries per day.
Do you have a Java Problem?
Ask It in The Java
Forum
Java Books
Java Certification,
Programming, JavaBean and Object Oriented Reference Books
Return to : Java
Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.erpgreat.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.erpgreat.com or the content authors.
|