java - Add time to 9-hours days -
i'm working on basic project manages tasks. working days 8:00 17:00. task has start time , estimated duration. estimated duration in minutes. want calculate end time.
i've figured out how add days start time far precise. divide estimated duration 540 (= 9 hours) know how many days there approximately are. want able calculate end date precisly.
i can't can't add minutes calendar because calendar uses 24-hours days instead of 9-hours days. example if start time 2015-04-26 16:00:00.0 , add 180 minutes (3 hours) end time 2015-04-27 10:00:00.0. how done? have far:
public timespan calculateestimatedduration() { simpledateformat format = new simpledateformat("yyyy-mm-dd hh:mm:ss.s"); //divide 540 because there 9 hours in work day long workingdays = math.round((this.getestimatedduration()/540)); //convert calendar add estimated working days start date date startdate = this.getplannedstarttime(); calendar cal = calendar.getinstance(); cal.settime(startdate); cal.add(calendar.date,(int) workingdays); string endtime = format.format(cal.gettime()); date plannedendtime = format.parse(endtime); return new timespan(this.getplannedstarttime(),plannedendtime); }
i've found solution using modulo operator, explanation in commentary:
public timespan addestimatedtotime(date time) { simpledateformat format = new simpledateformat("yyyy-mm-dd hh:mm:ss.s"); //first days long workingdays = this.getestimatedduration() / 540; //then remaining hours , minutes long remainderdays = this.getestimatedduration() % 540; //then calculate hours long workinghours = remainderdays/60; //and remaining minutes long workingminutes = remainderdays % 60; //convert calendar add estimated time given time calendar cal = calendar.getinstance(); cal.settime(time); cal.add(calendar.date,(int) workingdays); cal.add(calendar.hour , (int) workinghours); cal.add(calendar.minute, (int) workingminutes); //format date string endtime = format.format(cal.gettime()); date plannedendtime = format.parse(endtime); return new timespan(time,plannedendtime); }
Comments
Post a Comment