There are a couple ways I can think of. One way is to use *custom transform orientations*.

1. In edit mode (<kbd>&#8633; Tab</kbd>), select the edge that you want to align the object to and press <kbd>&#9096; Ctrl</kbd><kbd>&#9095; Alt</kbd><kbd>Space</kbd> to create a new transform orientation.

 The manipulator widget should align to the edge and you should see the selected transform orientation switch to the new custom one in *3D view > header*:

 ![enter image description here][1]

2. In object mode, select the object you wish to align and press *Align to transform orientation* in *3D view > Header > Object > Transform*:

 ![enter image description here][2]

3. Your object's local X Y and Z axes will be aligned to the active transform orientation. This is probably not exactly what you want. In your case, since you just want to adjust the rotation of the first object around the Z, set the rotation along the other two axes to 0:

 ![enter image description here][3]

4. Now we are closer, but still off by 90&deg; around the Z. To fix this, just add 90:

 ![enter image description here][4]

 


  [1]: https://i.sstatic.net/FamJa.png
  [2]: https://i.sstatic.net/PKZSL.png
  [3]: https://i.sstatic.net/ACACW.png
  [4]: https://i.sstatic.net/BiH2X.gif