The stock market is hovering just below record highs, so it's only natural for investors to wonder if it's still a good time to buy stocks, or if all of the good opportunities have passed. However, by applying some time-tested investment wisdom from the greatest investor of all time, you can set yourself up for success no matter which direction the market heads next.

Here are three lessons from Berkshire Hathaway (BRK.A 0.64%) (BRK.B 0.54%) CEO Warren Buffett that every investor should remember when the market looks frothy.

Warren Buffett in a crowd of people

Warren Buffett. Image source: The Motley Fool.

1. Invest with forever in mind

One piece of advice Buffett has given Berkshire Hathaway's own investors can be applied universally when the market seems expensive. In his 2014 letter to shareholders, Buffett wrote: "Since I know of no way to reliably predict market movements, I recommend that you purchase Berkshire shares only if you expect them to hold them for at least five years. Those who seek short-term profits should look elsewhere."

In other words, trying to time the market is a losing battle. Sure, the major market indexes are close to record highs right now, but the Dow Jones could continue to skyrocket to 20,000 just as easily as it could fall back to 17,000. We just don't know what will come next.

Because of this, while there is no guarantee of profit in the stock market, the best way to position yourself to make money is to hold the stocks you buy for a long period of time. I would even go so far as to recommend a minimum 10-year investment time frame. There have been very few instances throughout history where the overall stock market hasn't increased over any given 10-year period, and there have been no 15-year periods in which the overall market has declined.

^DJI Chart

^DJI data by YCharts.

In short, the most surefire way to make money in the stock market is to buy rock-solid companies and hold on -- the longer, the better.

2. Focus on great businesses at fair prices

Here's one of my all-time favorite Warren Buffett quotes: "It is far better to buy a wonderful business at a fair price than a fair business at a wonderful price."

This is especially true when the market is expensive and the bargain bin is all but empty. A rising market has never stopped Buffett and his stock pickers from finding good values in the market. Most notably, Berkshire bought a substantial amount of Apple (NASDAQ: AAPL) stock in the first quarter of 2016. It then added to that position in the second quarter after the stock dipped nearly 20% on declining revenue and tepid third-quarter guidance. Apple is one of the world's most valuable brand names, it has a dominant presence in its core markets, and it trades for just 12.5 times TTM earnings. Berkshire's stock pickers likely felt that Apple's long-term prospects far outweighed these short-term challenges.

Phillips 66 (NYSE: PSX) is another example of a stock Berkshire has been loading up on recently. If you compare it with other oil stocks, Phillips 66 isn't the cheapest. However, the company has a diverse revenue stream (some parts of the company actually do better with low oil prices), a solid balance sheet, great management, and a fair valuation.

The point here is not that you should follow Berkshire into Apple or Phillips 66. Rather, it's that you will rarely go wrong by purchasing stock in a rock-solid, industry-leading business when it looks fairly (not cheaply) valued.

3. Build your positions over time

Dollar-cost averaging, a favorite practice of Buffett's mentor Benjamin Graham, means investing a set dollar amount in the same investment at fixed intervals over time. Basically, this leads you to buy more shares when prices are low and fewer while prices are high. Buffett has said many times that the best way for the majority of people to invest is to dollar-cost average into a low-cost S&P 500 mutual fund. While I prefer buying individual stocks, dollar-cost averaging works no matter what type of investment you're looking at.

Consider this basic example. Let's say you have $20,000 to invest in a stock you like, which currently trades for $100 per share. Instead of spending all your available cash on shares right now, you spread it out over a year, purchasing $5,000 worth of shares each quarter. Here's how it pans out:

Quarter Purchase Price
Q1 $100
Q2 $105
Q3 $80
Q4 $85

In Q1, your $5,000 would have gotten you 50 shares, but in Q3, you would have bagged 62 shares for the same amount, lowering your average purchase price. This is a simplified example, because dollar-cost averaging typically involves spending a smaller amount of money on a monthly basis over a period of years or decades. But the same principle applies: Through simple math, dollar-cost averaging guarantees your average cost per share will be lower than the average market price of the stock at the times when you bought it.

As Graham once said, "Such a policy will pay off ultimately, regardless of when it is begun, provided that it is adhered to conscientiously and courageously under all intervening conditions." In other words, if the market looks expensive, you still have to make your scheduled investment. The same goes for when markets are crashing. To illustrate how this works, see another article in which I show how someone who had averaged into Bank of America over the past 10 years would be sitting on a profit today, even though the stock itself has fallen by nearly 70%.

BAC Chart

BAC data by YCharts.

From a long-term perspective, the stock market is still attractive

For all of the reasons mentioned here, the stock market is still an attractive place to invest. Sure, the fire-sale bargains of 2008 and 2009 aren't there anymore, but from a long-term perspective, that doesn't matter too much. Stick to a smart and consistent investment strategy no matter what the market is doing, and you'll end up a winner over time.