At the 2012 United States Olympic Trials in Omaha, Nebraska, the qualifying meet for the U.S. Olympic Team, Hansen won the 100-meter breaststroke final, qualifying him to swim in that event and the 4×100-meter medley relay at the 2012 Summer Olympics. He also competed in the 200-meter breaststroke and finished fourth. At the 2012 Summer Olympics in London, Hansen finished third in 100-meter breaststroke for a bronze medal, and won a gold medal as a member of the winning U.S. team in the 4×100-meter medley relay. At the conclusion of the games, Hansen decided that the 2012 Olympics would be his final meet, retiring from the sport of swimming.