There is a 180-day timeline that requires you invest that time period after the capital gain event. There are nuances for certain situations (i.e., partners in partnerships and net Section 1231 gains) whereby the 180 period does not start until after the end of the tax year during which a capital gain recognition event transpired.