Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculating a timespan between 2 timestamps  RSS feed

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
There are 2 pay rates - normal rates and overtime rates.
An employee can work in many different hour combination say
* 2012-10-30 08:00 to 2012-10-30 18:30
* 2012-10-30 10:30 to 2012-10-30 15:00
* 2012-10-30 07:00 to 2012-11-02 23:00

Despite of leaves and other allowances, how do you calculate their time spans of work
Say in the first day, he works from 7:00 - 23:59 and 2 full days and the last day 0:00 to 23:00 etc
My first thought was to convert this to a Calendar and compare the dates first and work out the full days worked.
if there is at least one full day, I subtract 23:59 from his on-duty time
and subtract his off-duty time from 0:00.
And then, I compare the range of time with the normal working-hours and OT hours
The problem is how do you convert a timestamp data type to calendar?
Am I on the right track?
Thanks
Jack
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I guess you are making the problem more complex.Check this out. Let me know if it helps

Cheers
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jack,
Please CarefullyChooseOneForum before posting. This question has nothing to do with Swing. So, I am moving this to the "Java in General" forum.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Shaw wrote:Hi,

I guess you are making the problem more complex.Check this out. Let me know if it helps

Cheers


I have to get specific hours that the employee is working in because they belong to certain pay rate categories....
Say if he enters the premise at 8:00
Then he gets one hour overtime pay salary. So the "hour" is signifiicant, don't you think?
Thanks
Jack
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Luk wrote:
Vishal Shaw wrote:Hi,

I guess you are making the problem more complex.Check this out. Let me know if it helps

Cheers


I have to get specific hours that the employee is working in because they belong to certain pay rate categories....
Say if he enters the premise at 8:00
Then he gets one hour overtime pay salary. So the "hour" is signifiicant, don't you think?
Thanks
Jack


I am still not clear of your requirement. Normally, there should be a specific time duration during which normal rates should apply. Any time excess of that should be qualified for overtime. So, if you can get the total working time in milliseconds, you can calculate total working hours,minutes etc.
Entering time has nothing to do with it, as he may enter an hour early and leave one hour early, so entering time is not significant in this case. Of course, it has a role for calculating the late arrivals, but you did not mentioned anything regarding it, so I assume it's not required. If my understanding of the requirement is wrong, then correct me.

But if you have decided to use Calendar only, have a look at this link. I am not sure how much help that would be to you. But you could have still done the job without using Calendar.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack, I think you should take a pencil and a paper and write down all the rules that govern the pay calculation in plain English. Generally there are many possibilities, ranging from fixed work schedule (with anything outside that schedule being special in some way) to "budgeted work hours", where only a month-total of worked hours is compared to the expected value, for example. You need to fully understand and describe your rules for computing the payments.

I believe they'll all translate into calculating an interval between two timestamps and work day (and perhaps state holiday) detection. Calculating interval between dates is really not that complicated. Nor is detecting weekends. I assume you'll probably need to handle the state holidays somehow, and as there is no standardized support for this (at least not in the JDK), you'll have to do that yourself.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Shaw wrote:
Jack Luk wrote:
Vishal Shaw wrote:Hi,

I guess you are making the problem more complex.Check this out. Let me know if it helps

Cheers


I have to get specific hours that the employee is working in because they belong to certain pay rate categories....
Say if he enters the premise at 8:00
Then he gets one hour overtime pay salary. So the "hour" is signifiicant, don't you think?
Thanks
Jack


I am still not clear of your requirement. Normally, there should be a specific time duration during which normal rates should apply. Any time excess of that should be qualified for overtime. So, if you can get the total working time in milliseconds, you can calculate total working hours,minutes etc.
Entering time has nothing to do with it, as he may enter an hour early and leave one hour early, so entering time is not significant in this case. Of course, it has a role for calculating the late arrivals, but you did not mentioned anything regarding it, so I assume it's not required. If my understanding of the requirement is wrong, then correct me.

But if you have decided to use Calendar only, have a look at this link. I am not sure how much help that would be to you. But you could have still done the job without using Calendar.


ehhh. Yes, you're right, duration. Because I cannot *directly* contact the customer. I have to send in the program and receive feedback from them. Thanks for the hints.
In other words, in case the one works for more than 9 hours, than the excess time will be regarded as OT.
Thanks
Jack
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allow me to open a new post.
I wonder it is possible to use some lexical or graphical analysers to analyse/model (if it is the correct word) business rules?

Update: I found drools
Thanks
Jack
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Luk wrote:Allow me to open a new post.
I wonder it is possible to use some lexical or graphical analysers to analyse/model (if it is the correct word) business rules?

Update: I found drools
Thanks
Jack


Let it be your business case:
Let's consider 2 special cases:
1) 8:00 - 21:00 , duration past 9 hours, 8:00-16:00, 16:00-21:00 is OT?
2) 16:00 - next day 4:00, duration past 9 hours, but it is in the next day.
How would your business consider such cases? Make it very general, cos these stuff is fairly standard.
Thanks
Jack
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!