Difference between 曾经 and 已经
I think it could be close to the fact to say &quot;曾经(once)“ is used in the past tense, while &quot;已经(already)&quot; is used in the perfect(past, present, or future, it doesn't matter) tense, that's why ”了&quot; is often used with it..
So when you are using the present perfect tense or future perfect tense, only 已经 can be used. For example(where 曾经 can't be used):

明年他就已经结婚5年了。 He will have been married for 5 years next year. Future Perfect Tense
他现在已经是这个公司的CEO了。 He has been the CEO of this company now. Present Perfect Tense

How about the past(past perfect) tense?
As you may have learned in English. the past perfect tense implies that the fact the sentence is talking about is related to or affect the status quo, and the past tense just states a fact. That's true to &quot;已经“ and &quot;曾经&quot;. ALso, 曾经 could implies that the fact doesn't last till now(see my answer to a related question &quot;What's the role of 曾经 in this sentence?&quot;) .For your examples:

他曾经在北京学过两年汉语。 State the fact that he studied Chinese in Beijing for 2 years and implies he is not studing Chinese in Beijing now.
他已经在北京学过两年汉语。Emphasize the fact that his experience in studing Chinese. This fact will affect the status quo, depending on the context.For example, when a company wants to hire a man who can speak Chinese and you go to the interview, you would prefer to say&quot;我已经在北京学过两年汉语&quot;. Here, you want the listener to understand that you can speak Chinese well.

Some other examples.

两年前他曾经是副经理。 He was(used to be) the deputy manager two years ago. Here, the sentence implies he is not the deputy manager now. So what's his title now? The speaker doesn't have any idea about this. The speaker just stated a fact.
两年前他已经是副经理了。 He had been the deputy manager two years ago Here, the sentence implies that he may get promoted now(perhaps he is the general manager now,in this case, you want to say he is talented), or he is still the deputy manger(in this case, you may want to say he should get promoted). All the implications depend on the context.

Let me show a conversation here. A policeman is investigating a case, and he is looking for a witness. He finds you and asks you some quesstions. You say,

You: 那天晚上我曾经在这儿看到他。 I saw him(the suspect) here on that night.
The policeman: 是吗？不过已经有人给我们报告过了。 Really? But someone else has informed us of this.(he is disappointed that you can't say something new).
You: 他曾经欠了A(the victim)一大笔钱，不过最近他好像突然有钱了。 He once owed a big sum of money to A, but it seems that he is rich now.
The policeman: 我们还不知道这个，我想我已经能确定他的作案动机了。We don't know this. Now I can say I have confirmed his motive to kill A