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.
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);
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.