Maybe depends where else you are planning on being. If Barcelona, there are several smaller towns with beaches not far away. If Madrid and other areas, then maybe the Costa Del Sol, basically I would avoid travelling far out of my way to get to a beach town, use what is close.
As for time of year, August will be extremely crowded at the beach and hotter than the balzes in Madrid, July not much better, June about the same.
May would be nice, water maybe still cold, September in general is a great time to travel.