Scala: Countdown to a birthday

//Save this as DaysTilBirthdays.scala
import java.time.LocalDate
import java.time.Month
import java.time.temporal.ChronoUnit.DAYS

object DaysTilBirthdays extends App {

	case class FamilyMember(name: String, birthday: LocalDate)
	val Frank = new FamilyMember("Frank", LocalDate.of(1980, Month.MAY, 1))
	val Tina = new FamilyMember("Tina", LocalDate.of(1984, Month.AUGUST, 12))
	val Ted = new FamilyMember("Ted", LocalDate.of(2006, Month.DECEMBER, 15))
	val Sally = new FamilyMember("Sally", LocalDate.of(2010, Month.SEPTEMBER, 21))
	val Anne = new FamilyMember("Anne", LocalDate.of(2015, Month.FEBRUARY, 1))

	val BrownFamily = List(Frank, Tina, Ted, Sally, Anne)
	val daysTilBirthdays = => (, DAYS.between(, p.birthday.withYear(2016))))
	daysTilBirthdays.foreach { case (name, days) => println(s"${name}'s birthday is in ${days} days") }


Quick real-life use case here to demonstrate how nice and concise Scala is. I wanted to quickly see how many days were between now and various family members’ birthdays. With just three lines of Scala for the calculation and a line to hold data for each of the family members, I was able to get a printout of how many days are left between today and their birthday.

You can obviously adapt this code for any noteworthy upcoming date.

Leave a Reply

Your email address will not be published. Required fields are marked *