TL; DR
To get the number 1-12 for the current month:
LocalDate.now() .getMonthValue()
It is better to indicate the desired / expected time zone.
LocalDate.now( ZoneId.of( "America/Montreal" ) ).getMonthValue()
.getYear() and .getDayOfMonth() .
More details
he returns the wrong month
As others have said, in the Calendar month January-December, 0-11 is insanely numbered , not 1-12. One of many bad design decisions in old time classes. These classes are now deprecated, superseded by java.time classes.
So is there any work around this?
Yes, there is a workaround. Use a good time library, not a clutter, which is java.util.Date/Calendar. The modern way is java.time classes.
This moment
The time zone is critical to getting the current date and time. At any given time and wall clocks vary depending on the zone.
ZoneId z = ZoneId.of( "America/Montreal" ); ZonedDateTime zdt = ZonedDateTime.now( z );
You can poll various components, such as year, month, localized month name through Month enum, from month.
System.out.println ( "Current: " + zdt ); System.out.println( "Year is " + zdt.getYear() ); System.out.println( "Month is " + zdt.getMonthValue() ); System.out.println( "Month name is " + zdt.getMonth().getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) );
Current: 2016-12-14T04: 54: 44.802-05: 00 [America / Montreal]
Year 2016
The month is 12
Name of the month décembre
Day 14
See live code at IdeOne.com .
If you only need a date, not a time of day, use the LocalDate class.
LocalDate.now( z );
Specific moment
You can specify the moment as the millisecond count since the epoch of the first moment of 1970 in UTC.
long input = 1_234_567_898_765L ; Instant instant = Instant.ofEpochMilli( input );
instant.toString (): 2009-02-13T23: 31: 38.765Z
Z in this release is short for Zulu and means UTC .
You can set the time zone to set for a specific wall time.
ZoneId z = ZoneId.of( "America/Montreal" ); ZonedDateTime zdt = instant.atZone( z );
zdt.toString (): 2009-02-13T18: 31: 38.765-05: 00 [America / Montreal]
See live code at IdeOne.com .
I do not recommend sharing date data this way. Better serialize text to ISO 8601 . For example: 2009-02-13T23:31:38.765Z
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .
The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .
Where to get java.time classes?
- Java SE 8 and SE 9 and later
- Built in.
- Part of the standard Java API with integrated implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Most of the functionality of java.time has been ported to Java 6 and 7 in ThreeTen-Backport .
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) specifically for Android.
- See How to use ....
The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .
Old Answer - Joda-Time
Update: Joda-Time project, now in service mode , we advise you to switch to the java.time classes.
Code example
Today
At startup ...
Current Year, Month & Day for: 2013-12-04T01:58:24.322-07:00 Year is 2013 Month is décembre Day is 4
Some day
At startup ...
Set Value of 1234567898765L is: 2009-02-13T23:31:38.765Z Year is 2009 Month is février Day of month is 13 Day of week is vendredi Day of year is 44
PS I just got chills on my back when I noticed that your randomly chosen Long led to Friday the Thirteenth!