3 Replies Latest reply on Jan 26, 2007 12:40 PM by Venkata mogili

    How to get the Business Hours:Min:Sec

    Venkata mogili Newbie

      Hi,

      Can anybody suggests on how to get the Business Hours:Min:Sec from two dates.

      Thanks
      Venkat

        • 1. Re: How to get the Business Hours:Min:Sec
          Edward Staub Expert

          Sorry, wrong forum - this is a Java question.

          -Ed Staub

          • 2. Re: How to get the Business Hours:Min:Sec
            Edward Staub Expert

            Venkat,

            My apologies - I misunderstood what you were asking about.

            As you probably know, BusinessCalendar has an add function, but not a subtract. The calendar package is not documented at all - not even any significant comments.

            I think you'll want to use BusinessCalendar.findDayPart to get the beginning and end time-segments for your interval, add the deltas for each end, and then sum the durations of all the intervening DayParts. It doesn't look too hard. You might consider contributing it back, especially if you write test code to make sure you got it right.

            -Ed Staub

            • 3. Re: How to get the Business Hours:Min:Sec
              Venkata mogili Newbie

              Ed,

              Thanks for the suggestion. I did some basic implemenation. Though it is not 100% complete , good for some body to start with. At this point the requirement was dropped from the project. I will update on this once I get chance to work again.

              Following is the code:


              import java.util.Calendar;
              import java.util.Date;

              public class TestCalendar {
              static int deltaHour = 0;
              static int deltaMinute = 0;
              static boolean flag= true;
              static boolean isFirstTime= true;

              public static void main(String args[]){
              System.out.println("Current Time ..."+new Date());
              //findBusinesstime();
              Calendar calendar = BusinessCalendar.getCalendar();
              calendar.set(Calendar.YEAR, 2007);
              calendar.set(Calendar.MONTH, Calendar.JANUARY);
              calendar.set(Calendar.DAY_OF_MONTH, 25);
              calendar.set(Calendar.HOUR_OF_DAY, 13);
              calendar.set(Calendar.MINUTE, 15);
              calendar.set(Calendar.SECOND, 0);
              calendar.set(Calendar.MILLISECOND, 0);
              Date startDay = calendar.getTime();
              System.out.println(startDay);

              Calendar calendar2 = BusinessCalendar.getCalendar();
              calendar2.set(Calendar.YEAR, 2007);
              calendar2.set(Calendar.MONTH, Calendar.JANUARY);
              calendar2.set(Calendar.DAY_OF_MONTH, 27);
              calendar2.set(Calendar.HOUR_OF_DAY, 13);
              calendar2.set(Calendar.MINUTE, 15);
              calendar2.set(Calendar.SECOND, 0);
              calendar2.set(Calendar.MILLISECOND, 0);
              Date endDay = calendar2.getTime();
              System.out.println(endDay);

              addHours("D:/workspace/SLA");

              }
              public static String getBusinessHours(BusinessCalendar businessCalendar,Date startDate, Date endDate){
              DayPart dayPart = null;

              Calendar calendar = BusinessCalendar.getCalendar();
              calendar.setTime(startDate);
              int hour = calendar.get(Calendar.HOUR_OF_DAY);
              int minute = calendar.get(Calendar.MINUTE);
              System.out.println("startDate------->"+startDate);
              System.out.println("endDate------->"+endDate);

              System.out.println("deltaHour------->"+deltaHour);
              System.out.println("deltaMinute------->"+deltaMinute);
              if(compareDates(startDate,endDate)== 1){ // First before day
              if (!businessCalendar.isHoliday(startDate)) {
              Day day = businessCalendar.findDay(startDate);
              for (int i=0; ((i < day.dayParts.length)
              && (dayPart==null)); i++) {
              DayPart dayPart1 = day.dayParts;
              DayPart dayPart2 = day.dayParts[++i];

              int fromHour1 = dayPart1.fromHour;
              int fromMinute1 = dayPart1.fromMinute;
              int toHour1 = dayPart1.toHour;
              int toMinute1 = dayPart1.toMinute;

              int fromHour2 = dayPart2.fromHour;
              int fromMinute2 = dayPart2.fromMinute;
              int toHour2 = dayPart2.toHour;
              int toMinute2 = dayPart2.toMinute;

              System.out.println("fromHour1------->"+dayPart1.fromHour);
              System.out.println("fromMinute1------->"+dayPart1.fromMinute);
              System.out.println("toHour1------->"+dayPart1.toHour);
              System.out.println("toMinute1------->"+dayPart1.toMinute);

              System.out.println("fromHour2------->"+dayPart2.fromHour);
              System.out.println("fromMinute2------->"+dayPart2.fromMinute);
              System.out.println("toHour2------->"+dayPart2.toHour);
              System.out.println("toMinute2------->"+dayPart2.toMinute);


              if(dayPart1.includes(startDate)){

              System.out.println("Included in DayPart1 ...");
              if(hour >= fromHour1 && minute !=0){
              deltaHour = deltaHour+((toHour1-hour)-1);
              deltaMinute = deltaMinute+((60-minute)+toMinute1);
              if(fromMinute2 !=0 || toMinute2!=0){
              deltaHour =deltaHour+ (toHour2-fromHour2)-1;
              deltaMinute =deltaMinute+ (60-(fromMinute2-toMinute2));
              }else{
              deltaHour =deltaHour+ (fromHour2-toHour2)-1;
              }

              }
              }

              System.out.println("------"+minute+":"+toMinute1+":"+hour+":"+toHour1);
              if(dayPart2.includes(startDate) ||((minute >= toMinute1 && minute <= fromMinute2) && hour == toHour1) ){

              System.out.println("Included in DayPart2 ..."+minute+":"+toMinute2+":"+deltaMinute);
              if(minute!=0){
              deltaHour =deltaHour+(toHour2-hour-1);
              deltaMinute =deltaMinute+ ((60-minute)+toMinute2);
              }else if(minute ==0){
              deltaHour =deltaHour+(toHour2-hour);
              deltaMinute =deltaMinute+toMinute2;
              }

              }
              if(deltaMinute>60){
              int j = deltaMinute/60;
              deltaHour = deltaHour+j;
              deltaMinute = deltaMinute-(j*60);
              }

              break;
              }
              }else{
              System.out.println("Holiday1 ------->");
              }
              }else if(flag && compareDates(startDate,endDate)== 0){ //For last SameDay case

              System.out.println("SameDay case ------->");
              if (!businessCalendar.isHoliday(startDate)) {
              Day day = businessCalendar.findDay(startDate);
              for (int i=0; ((i < day.dayParts.length)
              && (dayPart==null)); i++) {
              DayPart dayPart1 = day.dayParts
              ;
              DayPart dayPart2 = day.dayParts[++i];

              int fromHour1 = dayPart1.fromHour;
              int fromMinute1 = dayPart1.fromMinute;
              int toHour1 = dayPart1.toHour;
              int toMinute1 = dayPart1.toMinute;

              int fromHour2 = dayPart2.fromHour;
              int fromMinute2 = dayPart2.fromMinute;
              int toHour2 = dayPart2.toHour;
              int toMinute2 = dayPart2.toMinute;


              if(dayPart1.includes(startDate)){
              System.out.println("Included in DayPart1 ...");
              if(hour >= fromHour1){
              deltaHour = deltaHour+((hour-fromHour1)-1);
              deltaMinute = deltaMinute+((60-fromMinute1)+minute);
              }else {//non business hour
              deltaHour = deltaHour+((fromHour1-hour)-1);
              deltaMinute = deltaMinute+((60-minute)+fromMinute1);
              }
              }

              System.out.println("------"+minute+":"+toMinute1+":"+hour+":"+toHour1);
              if(dayPart2.includes(startDate) ||((minute >= toMinute1 && minute <= fromMinute2) && hour == toHour1) ){
              deltaHour =deltaHour+(hour-fromHour1-1);
              deltaMinute =deltaMinute+ ((60-fromMinute2)+minute);
              }
              if(deltaMinute>60){
              int j = deltaMinute/60;
              deltaHour = deltaHour+j;
              deltaMinute = deltaMinute-(j*60);
              }

              break;
              }
              }else{
              System.out.println("Holiday ------->");
              }
              flag = false;
              }else if(isFirstTime && compareDates(startDate,endDate)== 0){ // First time same day case ..
              Day day = businessCalendar.findDay(startDate);
              for (int i=0; ((i < day.dayParts.length)
              && (dayPart==null)); i++) {
              DayPart dayPart1 = day.dayParts;
              DayPart dayPart2 = day.dayParts[++i];

              int fromHour1 = dayPart1.fromHour;
              int fromMinute1 = dayPart1.fromMinute;
              int toHour1 = dayPart1.toHour;
              int toMinute1 = dayPart1.toMinute;

              int fromHour2 = dayPart2.fromHour;
              int fromMinute2 = dayPart2.fromMinute;
              int toHour2 = dayPart2.toHour;
              int toMinute2 = dayPart2.toMinute;


              if(dayPart1.includes(endDate)){
              System.out.println("endDate Included in DayPart1 ...");
              if(hour >= fromHour1){
              deltaHour = deltaHour+((hour-fromHour1)-1);
              deltaMinute = deltaMinute+((60-fromMinute1)+minute);
              }else {//non business hour
              deltaHour = deltaHour+((fromHour1-hour)-1);
              deltaMinute = deltaMinute+((60-minute)+fromMinute1);
              }
              }

              if(dayPart2.includes(startDate) ||((minute >= toMinute1 && minute <= fromMinute2) && hour == toHour1) ){
              deltaHour =deltaHour+(hour-fromHour1-1);
              deltaMinute =deltaMinute+ ((60-fromMinute2)+minute);
              }
              if(deltaMinute>60){
              int j = deltaMinute/60;
              deltaHour = deltaHour+j;
              deltaMinute = deltaMinute-(j*60);
              }

              break;
              }
              }
              isFirstTime = false;
              Date nextDay = businessCalendar.findStartOfNextDay(startDate);
              int sameday = compareDates(nextDay,endDate);
              if(flag && sameday !=0){
              if(sameday == 2){ //After
              System.out.println("After------->");
              flag = false;
              }else if(sameday == 1){//Subsequent before days ..
              flag = true;
              System.out.println("before------->");
              if (!businessCalendar.isHoliday(nextDay)){
              Day day = businessCalendar.findDay(nextDay);

              for (int i=0; ((i < day.dayParts.length)
              && (dayPart==null)); i++) {
              DayPart dayPart1 = day.dayParts
              ;
              DayPart dayPart2 = day.dayParts[++i];

              int fromHour1 = dayPart1.fromHour;
              int fromMinute1 = dayPart1.fromMinute;
              int toHour1 = dayPart1.toHour;
              int toMinute1 = dayPart1.toMinute;

              int fromHour2 = dayPart2.fromHour;
              int fromMinute2 = dayPart2.fromMinute;
              int toHour2 = dayPart2.toHour;
              int toMinute2 = dayPart2.toMinute;
              if(toMinute1!=0 || fromMinute1 !=0 ||fromMinute2!=0 ||toMinute2 !=0){
              deltaHour = deltaHour+(toHour1-fromHour1)+(toHour2-fromHour2)-1;
              int dayminutes = fromMinute1+toMinute1+fromMinute2+toMinute2;
              if(dayminutes > 60) dayminutes = dayminutes-60;
              else dayminutes = 60 - dayminutes;
              deltaMinute = deltaMinute+dayminutes;
              if(deltaMinute>60){
              int j = deltaMinute/60;
              deltaHour = deltaHour+j;
              deltaMinute = deltaMinute-(j*60);
              }
              }else{
              deltaHour = deltaHour+(toHour1-fromHour1)+(toHour2-fromHour2);
              }


              }

              }else{
              System.out.println("Holiday ------->");
              }
              }
              }

              if(flag){
              System.out.println("same day------->"+flag);
              //flag = false;
              isFirstTime = false;
              getBusinessHours(businessCalendar,nextDay,endDate);
              }
              //}


              System.out.println("deltaHour------->"+deltaHour);
              System.out.println("deltaMinute------->"+deltaMinute);
              return "";
              }
              public static int compareDates(Date d1, Date d2){

              if(d1.before(d2)){
              return 1;
              }else if(d1.after(d2)){
              return 2;
              }else{
              return 0;
              }
              }
              //public static Date addHours(String pf, Date d,Date d1, String s){
              public static void addHours(String pf){
              String propetiesFile = pf+"/"+"calendar.properties";
              BusinessCalendar businessCalendar = new BusinessCalendar(propetiesFile);
              Date startDate = new Date();
              Date endate = businessCalendar.add(startDate,new Duration("2 hours"));
              String businessHours = getBusinessHours(businessCalendar,startDate,endate);

              }

              }