code: public static double getBearingDifferenceDegrees(double bearing1, double bearing2) {
          if (bearing1 < 0)
              bearing1 += 360;
          if (bearing2 > 180)
              bearing2 -= 360;
          double result = bearing1 - bearing2;
          if (result > 180)
              result -= 360;
          return result;
      }
docs: returns difference in degrees in the range -180 to 180
  
  @param bearing1
  degrees between -360 and 360
  @param bearing2
  degrees between -360 and 360
  @return