On November 1st ..
Calendar.getInstance().get(Calendar.MONTH); // prints 10 (October)
It would make sense if we start with 0, but it appears that we do not
Calendar.getInstance().get(Calendar.JANUARY); // prints 1
What am i missing please?
The now() method of this class obtains the current date from the system clock. The getYear() method returns an integer representing the year filed in the current LocalDate object. The getMonth() method returns an object of the java. timeMonth class representing the month in the LocalDate object.
The getInstance() method in Calendar class is used to get a calendar using the current time zone and locale of the system. Syntax: public static Calendar getInstance() Parameters: The method does not take any parameters. Return Value: The method returns the calendar.
YearMonth ref = YearMonth. from(LocalDate. now()); return Month. from(temporal) == Month.
Calendar , too) are not officially deprecated, just declared as de facto legacy.
Months in Java Calendar are 0-indexed. Calendar.JANUARY
isn't a field so you shouldn't be passing it in to the get
method.
as others said Calendar.MONTH returns int and is zero indexed.
to get the current month as a String
use SimpleDateFormat.format()
method
Calendar cal = Calendar.getInstance();
System.out.println(new SimpleDateFormat("MMM").format(cal.getTime()));
returns NOV
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With