Java Gregorian calendar returns wrong month

So, I have been on this for several hours, and it returns the correct Year and Day, but for some odd reason, it returns the wrong month. I am sure this is a simple fix, but I cannot figure it out.

package gregoriancalendar; import java.util.GregorianCalendar; public class Calendar8_5 { public static void main(String[] args){ GregorianCalendar calendar = new GregorianCalendar(); System.out.println("Current Year, Month & Date: "); System.out.println("Year is " + calendar.get(1)); System.out.println("Month is " + calendar.get(2)); System.out.println("Day is " + calendar.get(5)); calendar.setTimeInMillis(1234567898765L); //Elapse Time System.out.println("Set Value of 1234567898765L"); System.out.println("Year is " + calendar.get(1)); System.out.println("Month is " + calendar.get(2)); System.out.println("Day is " + calendar.get(5)); } } 
0
java calendar gregorian-calendar
Dec 04 '13 at 5:24
source share
3 answers

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 ) ); // Or Locale.US, Locale.ITALY, etc. System.out.println( "Day is " + zdt.getDayOfMonth() ); 

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

 // © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so. // import org.joda.time.*; // Generally best to be explicit about time zone rather than depend on default. DateTimeZone denverTimeZone = DateTimeZone.forID( "America/Denver" ); java.util.Locale locale = Locale.FRANCE; DateTime now = new DateTime( denverTimeZone ); System.out.println( "Current Year, Month & Day for: " + now ); System.out.println( "Year is " + now.year().getAsText( locale ) ); System.out.println( "Month is " + now.monthOfYear().getAsText( locale ) ); System.out.println( "Day is " + now.dayOfMonth().getAsText( locale ) ); System.out.println(); // blank line. 

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

 // Not generally a good idea to focus on integers for working with date-time, but you asked for it. DateTime someDateTime = new DateTime( 1234567898765L, DateTimeZone.UTC ); System.out.println( "Set Value of 1234567898765L is: " + someDateTime ); System.out.println( "Year is " + someDateTime.year().getAsText( locale ) ); System.out.println( "Month is " + someDateTime.monthOfYear().getAsText( locale ) ); System.out.println( "Day of month is " + someDateTime.dayOfMonth().getAsText( locale ) ); System.out.println( "Day of week is " + someDateTime.dayOfWeek().getAsText( locale ) ); System.out.println( "Day of year is " + someDateTime.dayOfYear().getAsText( locale ) ); 

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!

+2
Dec 04 '13 at 9:02
source share

For some crazy reason, the Calendar class uses a zero-based index for the month (Jan == 0, Feb == 1, etc.), but all the other parts of the date are unidirectional (corresponding to their real numbers).

Presumably, this was done with some chronic enumeration attempt, but it's just plain stupid.

My advice is never to use Calendar. Use Joda-Time instead .

+3
Dec 04 '13 at 9:15
source share

Try the new date and time library provided by Java 8 , which is inspired by Joda-Time and they are easy to use. He also solved the problem with a month starting at 0.

0
Aug 12 '16 at 1:45
source share



All Articles