What a difference a year can make.

Suddenly, the anti-stop-and-frisk mayor who saw thousands of NYPD officers turn their backs on him just six months ago has done an about-face, offering spots for 1,300 new cops.

The swelling in the NYPD ranks is important to city safety and community-police relations. More cops are planned for street patrol. In recruiting the new officers, the emphasis is now on compassion instead of action and adventure.

City officials still have to reveal details regarding where the new cops will go and how the agreed-to cap on police overtime will work. And they should plan for what all this will mean to city budgets years from now.

But for Mayor Bill de Blasio, the move to hire more cops marks a stunning and welcome change. A year ago, when the City Council asked for 1,000 new cops, de Blasio balked and didn't include money for new officers in his budget.

This is the mayor who pressed for changes to stop-and-frisk, publicly embraced controversial cop critic the Rev. Al Sharpton, admitted to telling his son to be careful when dealing with the police and invoked the "black lives matter" rhetoric that many saw as anti-cop.

In December, cops turned their backs on de Blasio after Officers Wenjian Liu and Rafael Ramos were assassinated in Brooklyn. Tensions between the mayor and the police union rose, and words got ugly.

Since then, the mayor tried to repair his relationship with the NYPD and the Patrolmen's Benevolent Association. But even a month ago, he held fast to his mantra that no new cops were needed, saying he was "very confident in what's happening right now with the resources we have."

In his reversal, the mayor gave City Council Speaker Melissa Mark-Viverito and Police Commissioner Bill Bratton more than they publicly asked for. The city's budget will add 1,297 officers. About 300 will work in counterterrorism, while the rest will focus on neighborhood policing to improve relations between communities and cops, and increase the NYPD's presence in neighborhoods believed to be at risk.

Behind de Blasio's 180-degree shift is one concern: crime.

Shootings and homicides by gun are on the rise, NYPD statistics show. That's enough to scare many city residents -- but it's also enough to scare a mayor. A rise in crime ripples through quality of life, tourism, the economy and job creation. The city is built on how safe it is -- and, more significant, the perception of how safe it is. A few bad news notes -- and the house of cards could tumble.

De Blasio can't take that chance. So, he made a big move -- and it should pay off. City officials now need to focus on recruiting cops that reflect the city's diversity, particularly officers of color, as de Blasio suggested Tuesday. Then, they need to train the new cops well, place them where they can make the biggest difference and make sure the new recruits gain the trust of the communities they patrol.

More officers, particularly ones who build communities, should make for a better NYPD. And a mayor who awakens to the challenges and needs of his police department might even become a better mayor. Better late than never.