Skip to content

Instantly share code, notes, and snippets.

@reuniware
Last active July 31, 2019 13:02
Show Gist options
  • Save reuniware/fb6ca47a286639dfdab6d385236b1d8d to your computer and use it in GitHub Desktop.
Save reuniware/fb6ca47a286639dfdab6d385236b1d8d to your computer and use it in GitHub Desktop.
Android Kotlin Calendar get 1st day of year and last day of year
// first day of year
val cal = Calendar.getInstance()
cal.set(Calendar.MONTH, 0)
cal.set(Calendar.DAY_OF_MONTH, 1)
val year_start = cal.get(Calendar.YEAR)
val month_start = cal.get(Calendar.MONTH) // Mois de 0 à 11
val day_start = cal.get(Calendar.DAY_OF_MONTH)
textViewStartDate.setText("${String.format("%02d", day_start)}/${String.format("%02d", month_start+1)}/${String.format("%04d", year_start)}")
// last day of year
cal.set(Calendar.MONTH, 11)
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH))
val year_end = cal.get(Calendar.YEAR)
val month_end = cal.get(Calendar.MONTH) // Mois de 0 à 11
val day_end = cal.get(Calendar.DAY_OF_MONTH)
textViewEndDate.setText("${String.format("%02d", day_end)}/${String.format("%02d", month_end+1)}/${String.format("%04d", year_end)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment