0
Calendar now = Calendar.getInstance();
    
    System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
                        + "-"
                        + now.get(Calendar.DATE)
                        + "-"
                        + now.get(Calendar.YEAR));

why are we adding +1 to now.get(Calendar.MONTH) to get Current Date? Thanks in advance.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Sunil S R
  • 11
  • 1
  • 1
    because java month is zero based... January is considered zero. While in the real world, January is the first month (1) – Toerktumlare Jul 10 '21 at 10:48
  • 1
    I recommend you don’t use `Calendar`. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Also for printing it in a human friendly format use `DateTimeFormatter`. – Ole V.V. Jul 10 '21 at 11:06
  • 1
    Example: `LocalDate.now(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("M-d-y"))`. In my time zone it just gave `7-10-2021`. And there’s no funny adding 1. – Ole V.V. Jul 10 '21 at 11:14

0 Answers0