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.

3
  • 1
    because java month is zero based... January is considered zero. While in the real world, January is the first month (1) Commented Jul 10, 2021 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. Also for printing it in a human friendly format use DateTimeFormatter. Commented Jul 10, 2021 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. Commented Jul 10, 2021 at 11:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.