Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

Two New York City police officers were shot within hours of each other Saturday night and Sunday morning in what officials believe were targeted attacks by the same gunman.

The alleged gunman was identified Sunday as Robert Williams, 45, who was arrested on suspicion of attempted murder, criminal possession of a weapon and resisting arrest.

The first officer was wounded when a gunman fired into his patrol van Saturday night, an attack that officials called an attempted assassination. The second officer was shot Sunday morning when a gunman entered a precinct headquarters in the Bronx and opened fire with a 9 mm handgun, Police Commissioner Dermot Shea said.

Surveillance video showed the shooter firing toward a desk where there were several uniformed officers, Shea said. From point-blank range, the gunman then fired several more rounds at other officers and a civilian, he added.

A lieutenant was struck in his upper left arm before the gunman surrendered and was taken into custody, Shea said. The officer was listed as stable, New York Mayor Bill de Blasio tweeted Sunday morning.

"It is only by the grace of God and the heroic actions of those inside the building that took him into custody that we are not talking about police officers murdered inside a New York City police precinct," Shea said.

Shea said the suspect was convicted in 2002 of attempted murder after he shot a person, carjacked a woman and opened fire on police officers. The suspect was paroled in 2017, Shea said.

Authorities believe he was responsible for both shootings.

The ambush in the van, which Shea said "should outrage all New Yorkers," happened just before 8:30 p.m. in the South Bronx.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

The officer at the wheel was grazed in his chin and his neck, but he avoided serious injury, Shea said Saturday. He was expected to released from the hospital Sunday.

Download the NBC News app for breaking news

"He is lucky to be alive," Shea said Saturday of the officer who was wounded Saturday night. "He is expected to make a full recovery, and it is a miracle."

Shea said the shooting "hearkens back to some very bad times," recalling other unprovoked assaults on police officers sitting in their patrol vehicles.

In 2017, a gunman killed Officer Miosotis Familia as she sat in her patrol vehicle in the Bronx. In 2014, two officers, Rafael Ramos and Wenjian Liu, were shot dead in their patrol car in Brooklyn by a man upset about recent police killings of unarmed black men. Shea said Ramos' and Liu's deaths were "not something that engenders anything but the worst memories."

De Blasio condemned the van attack at a news briefing Saturday night outside the Bronx hospital where the wounded officer was being treated.

"There's too much hatred in general, there's too much hatred being directed at our officers, and it has to end," he said. "We have to move forward in a situation like this and find a way to create a peaceful society, not one where those who protect us are in danger in this way."

The two uniformed officers, partners for eight years and friends since middle school, were sitting in the van with emergency lights activated when a man approached them and engaged them in conversation, Shea said.

The man asked the officers for directions and then pulled out a gun "without provocation," Shea said. The man fired multiple shots, striking the officer behind the wheel. Shea said the officer's carotid artery narrowly avoided injury.

Neither officer returned fire. The officer's partner drove him to a hospital nearby. Shea called both officers "heroic" for their composure and said their long association made for "an amazing story."

The officers had been stationed in the neighborhood because of recent drug activity and violence, Shea said.

President Donald Trump reacted to the shootings Sunday morning by tweeting that the New York Police Department was "under assault" because of the "weak leadership" of de Blasio and New York Gov. Andrew Cuomo, both Democrats.

The Sergeants Benevolent Association ratcheted up the rhetoric by saying its members, active and retired NYPD sergeants, were "declaring war" on City Hall.

"Mayor DeBlasio, the members of the NYPD are declaring war on you!" according to an SBA tweet. "We do not respect you, DO NOT visit us in hospitals. You sold the NYPD to the vile creatures, the 1% who hate cops but vote for you. NYPD cops have been assassinated because of you. This isn’t over, Game on!"

The union's harsh statement came as a retweet from a de Blasio statement backing police and bemoaning this "premeditated assassination attempt against New York’s Finest" that was an attack on all "New Yorkers and everything we believe in."