As the only method for all-time, all-weather, and long-distance environmental monitoring and target detection, radar is considered an essential sensor for future intelligent applications. However, electronic radars face critical challenges in terms of frequency range, bandwidth, and processing speed, leading to poor resolution, slow response, and being easy to be interfered. A microwave photonic cognitive radar with a subcentimeter resolution is reported, adaptively utilizing opportunistic spectra in a potential hundreds-of-GHz range. The concept is enabled by a photonic short-time Fourier transformation module for real-time wideband electromagnetic environmental monitoring, a photonic programmable Fourier-domain waveform synthesizer, and processor for target detection. Radar imaging is implemented over 1-40 GHz in an environment with interference signals. Both the spectra and the frequency-time characteristics are obtained for the environmental perception, and reconfigurable microwave signals are generated and processed accordingly. Using photonic dechirp processing with a 22-GHz bandwidth and an ADC with a 50-MSa/s sampling rate, the radar ranging resolution reaches 0.73 cm. This work provides an effective solution to overcome the limitations of the current radars on frequency range, bandwidth, and processing speed, which may enable all-time, all-weather sensors with ultrahigh resolution and fast response for automatic driving, security surveillance, space debris detection, etc.