President Biden and former President Donald J. Trump on Tuesday secured the delegates necessary to clinch their parties’ presidential nominations, according to The Associated Press, cementing a general election rematch in November months in the making.

Both men and their campaigns have long anticipated this moment. Mr. Biden faced only token opposition in the Democratic primary, as is typical for a sitting president, while Mr. Trump had been his party’s dominant front-runner for months.

Their November collision began to look even more likely after Mr. Trump scored a decisive win in Iowa in January. His victory cleared the field of all but one of his major Republican rivals and put him on a glide path to his party’s nomination. His last remaining primary challenger, Nikki Haley, suspended her campaign last week, further clearing a path that had already been remarkably free of obstacles for a candidate facing considerable legal problems.

The Associated Press named Mr. Biden the presumptive Democratic nominee on Tuesday after projecting his victory in Georgia, while Mr. Trump was designated the presumptive Republican nominee after he swept the G.O.P. contests in Georgia, Mississippi and Washington State. Later, Mr. Trump captured the Republican caucuses in Hawaii.

Tuesday’s results cleared the way for a 2024 general election campaign that, at just under eight months, is set to be one of the longest in modern American history and will be the country’s first presidential rematch in nearly 70 years.

Already, Mr. Trump and Mr. Biden had shifted their focus away from the primaries. With the president facing no significant challengers, Mr. Biden’s campaign speeches emphasized not just his record but the danger he believes is posed by Mr. Trump.

In a statement, Mr. Biden said he was honored that Democratic voters “have put their faith in me once again to lead our party — and our country — in a moment when the threat Trump poses is greater than ever.”

And even as Mr. Trump was working to dispatch his Republican rivals, his campaign speeches centered on criticisms of Mr. Biden and his insistence that the primary needed to come to a swift end so that his party could focus its energy and resources on November.

In a video posted on social media by his campaign after he clinched the nomination, Mr. Trump called Tuesday a “great day of victory,” but said it was immediately time to focus on defeating Mr. Biden in November. “I want to thank everybody, but much more importantly, we have to get to work to beat Joe Biden,” he said.

Neither man will be formally selected until his party’s conventions this summer. But Mr. Biden has already been using the political and financial apparatus of the Democratic National Committee. And last week, the Trump campaign effectively took over the Republican National Committee, imposing mass layoffs on Monday as it reshapes the party’s operations.

That Mr. Trump was able to lock up the Republican nomination fairly quickly demonstrates the grasp he has kept on the party and his conservative base, despite his 2020 loss and failed efforts to overturn it; a string of disappointing midterm losses by candidates he backed; and his 91 felony charges in four criminal cases.

The former president won nearly every nominating contest that awarded delegates, with Ms. Haley scoring wins in only Vermont and Washington, D.C., where she became the first woman to ever win a Republican presidential primary or caucus.

But Mr. Trump’s swift path to the nomination also reflects a backroom effort by him and his political team to bend rules around primaries and delegates in his favor. The rules that states use to award delegates to particular candidates are decided by state party officials, and Mr. Trump and his advisers built relationships with those officials to ease his path.

In one critical example, Mr. Trump’s campaign worked to shape California’s rules, leading party officials there to adopt a “winner take all” system that would award the state’s delegates to a candidate who swept 50 percent of the vote statewide. That threshold favored Mr. Trump, the only candidate polling at that level there.

Mr. Trump ultimately won California’s primary last week, a major moment in the delegate race. California’s 169 delegates gave him 14 percent of the 1,215 delegates needed to win the nomination.

Similarly, Mr. Biden faced little opposition in his march to the nomination, dominating every contest by wide margins. Robert F. Kennedy Jr., the political scion and environmental lawyer, dropped out of the Democratic nominating contest to run as an independent. Representative Dean Phillips of Minnesota and the self-help guru Marianne Williamson never attracted more than a fraction of the vote.

Both men’s strength in their primaries may belie weaknesses within their coalitions that could pose difficulty for them in November, particularly given that the 2020 election was decided by narrow margins in just a handful of states.

In some places where Mr. Trump won the Republican contests convincingly, he still performed comparatively weaker with voters in suburban areas and those who identify as moderates or independents. Such groups, whose support Mr. Trump lost in 2020, may be crucial in tightly contested battleground states.

Mr. Biden, for his part, faced a campaign in several primary states that urged voters to protest his handling of Israel’s war in Gaza by voting “uncommitted.” Losing the support of those voters in the fall could weaken the coalition that helped Mr. Biden oust Mr. Trump in 2020.

During Mr. Biden’s first term, voters have questioned his age and his record, even as economic indicators improve. The president has shown weakness with young people and Black and Hispanic voters, key groups in the coalition that powered him to victory last time around.

Mr. Biden is viewed unfavorably by a majority of Americans — a precarious position for a president seeking re-election — although so is Mr. Trump.

Both campaigns have argued that voters who backed them in previous years will return to them as the choice crystallizes.

Mr. Biden and his allied groups also have a significant financial advantage over Mr. Trump, whose legal bills are taking a toll.

With Tuesday’s victories, Mr. Trump has locked up the nomination before any of his four criminal cases have proceeded to trial. His Manhattan criminal case, which stems from a hush-money payment to a porn star in 2016, is set to go to trial on March 25 and is expected to last six weeks.

Mr. Trump’s lawyers had argued unsuccessfully that the timing would interfere with his presidential campaign, pointing in part to the primary calendar.

More recently, Mr. Trump’s legal team made a last-ditch effort to delay the trial before it starts. In court papers made public on Monday, his lawyers argued that the trial should not take place until the Supreme Court has decided whether Mr. Trump is immune from prosecution in his Washington criminal case, which involves accusations that he plotted to overturn the 2020 election.

The judge in the New York case, Juan M. Merchan, is unlikely to grant the request.