You can buy your ticket online, but if you're travelling on a regional train you'll need to validate it – the ticket will be valid for 4 hours. If you're travelling on a long-distance trip, your ticket will be valid from the moment it's validated until 05:00 the following day.