import java.util.{ Calendar, GregorianCalendar }
import language.postfixOps
import collection.mutable.ListBuffer

object CalendarPrint extends App {
  val locd = java.util.Locale.getDefault()
  val cal = new GregorianCalendar
  val monthsMax = cal.getMaximum(Calendar.MONTH)

  def JDKweekDaysToISO(dn: Int) = {
    val nday = dn - Calendar.MONDAY
    if (nday < 0) (dn + Calendar.THURSDAY) else nday
  }

  def daysInMonth(year: Int, monthMinusOne: Int): Int = {
    cal.set(year, monthMinusOne, 1)
    cal.getActualMaximum(Calendar.DAY_OF_MONTH)
  }

  def namesOfMonths() = {
    def f1(i: Int): String = {
      cal.set(2013, i, 1)
      cal.getDisplayName(Calendar.MONTH, Calendar.LONG, locd)
    }
    (0 to monthsMax) map f1
  }

  def offsets(year: Int): List[Int] = {
    val months = cal.getMaximum(Calendar.MONTH)
    def get1stDayOfWeek(i: Int) = {
      cal.set(year, i, 1)
      cal.get(Calendar.DAY_OF_WEEK)
    }
    (0 to monthsMax).toList map get1stDayOfWeek map { i => JDKweekDaysToISO(i) }
  }

  def headerNameOfDays() = {
    val mdow = cal.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.SHORT, locd)