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.
Calendar. That class is poorly designed and long outdated. Instead useLocalDatefrom java.time, the modern Java date and time API. Also for printing it in a human friendly format useDateTimeFormatter.LocalDate.now(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("M-d-y")). In my time zone it just gave7-10-2021. And there’s no funny adding 1.