datetime - Need Help on Computing spilled time in JAVA -


good day all! badly needed on this. have requirement wherein have calculate estimated start/end time of tasks. given figures in image below, how in java? please note new java, please don't yell @ me on :(

this desired output want :)

**this i've done far based on master madprogrammer**.  localtime openingtime = localtime.of(8, 0); localtime closingtime = localtime.of(18, 0); localdatetime jobstartat = localdatetime.of(2012, 7, 31, 8, 0); localdatetime closingat = localdatetime.of(2012, 7, 31, 18, 0); long minutesduration = (long) 12*60;  system.out.println("start " + printdatetime(jobstartat)); jobstartat = getspilledtime(openingtime, closingtime, closingat, jobstartat, minutesduration); system.out.println("end " + printdatetime(jobstartat));   private static localdatetime getspilledtime(localtime openingtime,         localtime closingtime, localdatetime closingat,         localdatetime jobstartat, long minutesduration) {      localdatetime estimatedendtime = jobstartat             .plusminutes(minutesduration);      if (estimatedendtime.isafter(closingat)             || estimatedendtime.isequal(closingat)) {         localdatetime falloverstartat = jobstartat;         {             duration duration = duration.between(closingat,                     estimatedendtime);             estimatedendtime = localdatetime.of(                     falloverstartat.tolocaldate(), closingtime);              falloverstartat = falloverstartat.plusdays(1);             falloverstartat = localdatetime.of(                     falloverstartat.tolocaldate(), openingtime);             estimatedendtime = falloverstartat                     .plushours(duration.tohours());              closingat = closingat.plusdays(1);         } while (estimatedendtime.isafter(closingat));      } else {         // job start on/at jobstartat         // , end on/at estimatedendtime     }      jobstartat = estimatedendtime;      return jobstartat; } 

start become familiar java 8's time apis, java.time framework.

you're going spending bit of time here.

next, know have...

  • a "start date" (01/01/2017)
  • a "start of business" time (08:00am)
  • a "end of business" time (06:00pm)
  • a "duration in hours" of each task

what need know is, when task's expected end time exceeds "end of business" time , how much, requires add remaining time "start of business" of next day , repeat process until estimated end time before "end of business" time

let's start basic "known" information...

localtime openingtime = localtime.of(8, 0); localtime closingtime = localtime.of(18, 0);  localdate startat = localdate.of(2017, month.january, 1); 

then based on information, can take duration of task , calculate estimated end date/time

localdatetime estimatedendtime = jobstartat.plushours(task.getdurationinhours()); if (estimatedendtime.isafter(closingat) || estimatedendtime.isequal(closingat)) {     localdatetime falloverstartat = jobstartat;     {         duration duration = duration.between(closingat, estimatedendtime);         estimatedendtime = localdatetime.of(falloverstartat.tolocaldate(), closingtime);          falloverstartat = falloverstartat.plusdays(1);         falloverstartat = localdatetime.of(falloverstartat.tolocaldate(), openingtime);         estimatedendtime = falloverstartat.plushours(duration.tohours());         closingat = closingat.plusdays(1);     } while (estimatedendtime.isafter(closingat));     // job start on/at jobstartat     // , end on/at estimatedendtime } else {     // job start on/at jobstartat     // , end on/at estimatedendtime }  // next job starts here jobstartat = estimatedendtime; 

okay, conceptually, is, starting @ given point in time, adds number of hours it. checks see if time greater or equal closing of business, if is, loops, adding "overflow" each day start of business next day until work finishes before close of business.

you have start time/date in jobstartat , end time/date in estimatedendtime.

the estimatedendtime becomes next job's start date/time , repeat until complete.

how if business hours have multiple entries? lets have these: 8:00am-12:00pm , 2:00:pm-6:00pm. - how able tweak code?

...i'd tell employer i'm available paid work...

okay, let's turn problem on it's head, know opening time (08:00am) , closing time (06:00pm) gives ten hours. know there @ least 1 break (12pm-2pm) of 2 hours, leaves window of 8 available hours in day.

now, need algorithm squeeze work available time each day, allow tasks start @ end of previous task...

so again, start initial data...

    localtime openingtime = localtime.of(8, 0);     localtime closingtime = localtime.of(18, 0);     // list of breaks, starting @ ending @     // way, can add more breaks     list<localtime[]> breaks = new arraylist<>();     breaks.add(new localtime[]{localtime.of(12, 0), localtime.of(14, 0)});      localdate startat = localdate.of(2017, month.january, 1);      localdatetime startdatetime = startat.attime(openingtime); 

now, need calculate available amount of time complete work...

    duration wholeday = duration.between(openingtime, closingtime);     (localtime[] hole : breaks) {         wholeday = wholeday.minus(duration.between(hole[0], hole[1]));     } 

then, need messed way figure how squeeze each job available time...

public localdatetime getdatetimetofit(long duration, duration wholeday, localdatetime jobstartat, localtime openingtime, localtime closingtime) {      localdatetime estimatedendtime = jobstartat;     {         // basically, we're going calculate out         // number of days , hours job         // require completed within available         // time of whole day...         int days = (int) (duration / wholeday.tohours());         int hours = (int) (duration % wholeday.tohours());         if (hours == 0) {             days--;             hours += wholeday.tohours();         }          estimatedendtime = jobstartat.plusdays(days).plushours(hours);         localdatetime closingat = estimatedendtime.tolocaldate().attime(closingtime);         // how overflow there @ end of         // day, if it's more 0, need cycle around         // again , squeeze more time of next day         duration fallover = duration.between(closingat, estimatedendtime);         duration = fallover.tohours();          // increment day here because it's convenient         // so, if exit loop, won't make         // difference         jobstartat = estimatedendtime.plusdays(1).tolocaldate().attime(openingtime);      } while (duration > 0);      return estimatedendtime;  } 

based on available durations of int[] durations = new int[]{12, 8, 15};, gives me following output...

2017-01-01t08:00 - 2017-01-02t12:00 2017-01-02t12:00 - 2017-01-03t10:00 2017-01-03t10:00 - 2017-01-04t17:00 

now, warn you, "conceptual" idea, hacked out late @ night when should have gone bed, going need sit down , come adequate test data , known results , test properly, making tweaks go.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -