-1
    Calendar now = null;
    now.getInstance();
    System.out.println(now.getInstance());
    System.out.println(now.ERA);
    System.out.println(now.YEAR);
    System.out.println(now.MONTH);
    System.out.println(now.WEEK_OF_YEAR);
    System.out.println(now.WEEK_OF_MONTH);
    System.out.println(now.DAY_OF_MONTH);
    System.out.println(now.DAY_OF_YEAR);
    System.out.println(now.DAY_OF_WEEK);
    System.out.println(now.DAY_OF_WEEK_IN_MONTH);

Program outputs (important information is in caps and on the new line):

java.util.GregorianCalendar[time=1412554865330,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=9,WEEK_OF_YEAR=41,WEEK_OF_MONTH=2,DAY_OF_MONTH=5,DAY_OF_YEAR=278,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=5,HOUR_OF_DAY=17,MINUTE=21,SECOND=5,MILLISECOND=330,ZONE_OFFSET=-28800000,DST_OFFSET=3600000]

0
1
2
3
4
5
6
7
8

(0-8 are on new lines) I'm really loss here and am not sure why java decided play this awful prank on me.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
David Fisher
  • 282
  • 2
  • 13
  • 1
    What is the problem, exactly? What are you seeing that you don't expect? Perhaps the MONTH value? [*The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.)*](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#MONTH) – Greg Hewgill Oct 06 '14 at 00:27
  • 1
    Oh, I see what your problem is. Try `System.out.println(now.get(Calendar.YEAR));`. Also, `Calendar now = Calendar.getInstance();`. – Greg Hewgill Oct 06 '14 at 00:29

3 Answers3

3

Those are all static fields from the Calendar class. Java allows static fields (and methods) to be accessed on expressions that resolve to an instance's reference value the same way it does on type names

now.ERA
// is equivalent to
Calendar.ERA

The Calendar class provides a get(int) method for getting the value of a date field.

now.get(Calendar.MONTH);
Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I've tried multiple ways of writing this including Calendar.getInstance().MONTH(). However the way you suggested causes a NullPointerException. Calendar now = null; now.get(Calendar.MONTH); because now is null but must be initialized in order to use its methods. – David Fisher Oct 06 '14 at 00:40
  • 1
    @DavidFisher I've linked the javadoc. Don't just guess. Look at the available methods and use them. I've already supplied one example of getting the Month field from the `Calendar` instance. – Sotirios Delimanolis Oct 06 '14 at 00:43
  • @DavidFisher Initialize your reference before you invoke a method on it. – Sotirios Delimanolis Oct 06 '14 at 00:43
  • I'm a moron. Initialize by Calendar now = Calendar.getInstance(); Thank you for your help. – David Fisher Oct 06 '14 at 00:45
0

Joda-Time

Just FYI, you might find working with the Joda-Time library (or the java.time package in Java 8) to be a more pleasant experience than using the java.util.Date & java.util.Calendar classes.

Both Joda-Time and java.time follow ISO 8601 for defining week-of-year and for default string formats.

Here is the same kind of code but using Joda-Time 2.4.

DateTimeZone timeZone = DateTimeZone.forID( "America/Montreal" );
java.util.Locale locale = Locale.CANADA_FRENCH;
DateTimeFormatter formatter = DateTimeFormat.forStyle( "FF" ).withLocale( locale );
DateTime dateTime = DateTime.now( timeZone );

Dump to console.

System.out.println( "Full: " + formatter.print( dateTime ) );
System.out.println( "Era: " + dateTime.getEra() ); // 0 (BC/BCE) & 1 (AD/CE).
System.out.println( "Year: " + dateTime.getYear() );
System.out.println( "Month: " + dateTime.getMonthOfYear() );
System.out.println( "WeekOfYear: " + dateTime.getWeekOfWeekyear() ); // Standard ISO 8601 week.
//System.out.println( now.WEEK_OF_MONTH ); // Not in Joda-Time as there is no standard definition for week-of-month.
System.out.println( "DayOfMonth: " + dateTime.getDayOfMonth() );
System.out.println( "DayOfYear: " + dateTime.getDayOfYear() );
System.out.println( "DayOfWeek: " + dateTime.getDayOfWeek() ); // Starts at 1 rather than 0. Amazing!
//System.out.println( now.DAY_OF_WEEK_IN_MONTH ); // Not in Joda-Time as there is no standard definition for week-of-month.
System.out.println( "TimeZone: " + dateTime.getZone() );

When run.

Full: dimanche 5 octobre 2014 21 h 22 EDT
Era: 1
Year: 2014
Month: 10
WeekOfYear: 40
DayOfMonth: 5
DayOfYear: 278
DayOfWeek: 7
TimeZone: America/Montreal
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

I recommend Java8's LocalDateTime and LocalTime classes if you can port into Java8

Otherwise I'd recommend using GregorianCalendar cal = new GregorianCalendar()

cal.get(Calendar.MONTH) for example
Muli Yulzary
  • 2,559
  • 3
  • 21
  • 39