public static boolean rotate(int[][] matrix) {
    int n = matrix.length;
    if (n == 0 || n != matrix[0].length) return false; // Not a square
    
    for (int layer = 0; layer < n/2; layer++) {
        int first = layer;
        int last = n - 1 - layer;
        for(int i = first; i < last; i++) {
            int offset = i - first;
            int top = matrix[first][i]; // save top

            // left -> top
            matrix[first][i] = matrix[last-offset][first]; 			

            // bottom -> left
            matrix[last-offset][first] = matrix[last][last - offset]; 

            // right -> bottom
            matrix[last][last - offset] = matrix[i][last]; 

            // top -> right
            matrix[i][last] = top; // right <- saved top
        }
    }
    return true;
}

public static void main(String[] args) {
    int[][] matrix = AssortedMethods.randomMatrix(3, 3, 0, 9);
    AssortedMethods.printMatrix(matrix);
    rotate(matrix);
    System.out.println();
    AssortedMethods.printMatrix(matrix);
}