user3062776
user3062776

Reputation: 167

calendar vs gregorian in java

Here is stuff:

http://www.javalaunch.com/Java-Date-Calender-time-2.html

Anyone can tell me what is the logic behind this Calendar.YEAR and calender.get(Calendar.YEAR).Actually i dont understand this .

         System.out.println("Year: " + Calendar.YEAR);

        System.out.println("month: " + Calendar.MONTH);

        System.out.println("dayOfMonth: " + Calendar.DAY_OF_MONTH); // Jan = 0, not 1

        System.out.println("dayOfWeek: " + Calendar.DAY_OF_WEEK);

        System.out.println("weekOfYear: " + Calendar.WEEK_OF_YEAR);

        System.out.println("weekOfMonth: " + Calendar.WEEK_OF_MONTH);

        System.out.println("hour: " + Calendar.HOUR); // 12 hour clock

        System.out.println("hourOfDay: " + Calendar.HOUR_OF_DAY); // 24 hour clock

        System.out.println("minute: " + Calendar.MINUTE);

        System.out.println("second: " + Calendar.SECOND);

        System.out.println("millisecond: " + Calendar.MILLISECOND);
        System.out.println("...................................................................................");

        Calendar calender = new GregorianCalendar();

        System.out.println("Year: " + calender.get(Calendar.YEAR));

        System.out.println("month: " + calender.get(Calendar.MONTH));

        System.out.println("dayOfMonth: " + calender.get(Calendar.DAY_OF_MONTH)); // Jan = 0, not 1

        System.out.println("dayOfWeek: " + calender.get(Calendar.DAY_OF_WEEK));

        System.out.println("weekOfYear: " + calender.get(Calendar.WEEK_OF_YEAR));

        System.out.println("weekOfMonth: " + calender.get(Calendar.WEEK_OF_MONTH));

        System.out.println("hour: " + calender.get(Calendar.HOUR)); // 12 hour clock

        System.out.println("hourOfDay: " + calender.get(Calendar.HOUR_OF_DAY)); // 24 hour clock

        System.out.println("minute: " + calender.get(Calendar.MINUTE));

        System.out.println("second: " + calender.get(Calendar.SECOND));

        System.out.println("millisecond: " + calender.get(Calendar.MILLISECOND));

OutPut:

Year: 1
month: 2
dayOfMonth: 5
dayOfWeek: 7
weekOfYear: 3
weekOfMonth: 4
hour: 10
hourOfDay: 11
minute: 12
second: 13
millisecond: 14
...................................................................................
Year: 2014
month: 11
dayOfMonth: 24
dayOfWeek: 4
weekOfYear: 52
weekOfMonth: 4
hour: 11
hourOfDay: 11
minute: 51
second: 54
millisecond: 687

Upvotes: 2

Views: 513

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 339679

Avoid terrible legacy date-time classes

Anyone can tell me what is the logic behind this Calendar.YEAR and calender.get(Calendar.YEAR).

You will not find much logic in the Calendar, GregorianCalendar, Date, SimpleDateFormat, and other legacy date-time classes. These terribly flawed classes were designed by people who did not understand date-time handling. Never use these.

Actually i dont understand this .

Not your fault. These classes are a bloody mess. Rather than spend energy on trying to understand them, just dump them. They have been entirely supplanted by the modern java.time classes built into Java 8+, as defined in JSR 310.

java.time.LocalDate

For a date-only value, without time-of-day and without time zone or offset, use the java.time.LocalDate class.

Getting today’s date requires a time zone. For any given moment, the time and the date vary around the globe by time zone.

ZoneId z = ZoneId.of( "America/Edmonton" ) ;
LocalDate today = LocalDate.now ( z ) ;

You may interrogate for its parts by calling get… methods.

int year = today.getYear() ;
int monthNumber = today.getMonthValue() ;  // Sane numbering: 1-12 for January-December.
Month month = today.getMonth() ;  // Enum object. JANUARY-DECEMBER.
int dayOfMonth = today.getDayOfMonth() ;
DayOfWeek dow = today.getDayOfWeek() ;  // Enum object. MONDAY-SUNDAY.
int dayOfYear = today.getDayOfYear() ;  // From 1 to 365, or 366 in a leap year.

… and more.

java.time.ZonedDateTime

For a date with time-of-day as seen in a particular time zone, use ZonedDateTime class. This class offers many methods to those seen above.

ZonedDateTime nowEdmonton = ZonedDateTime.now ( z ) ;
int year = nowEdmonton.getYear() ;
…

Upvotes: 1

eatSleepCode
eatSleepCode

Reputation: 4637

When your using (Calendar.YEAR, Calendar.MONTH), these are the static fields in the Calendar class and if you are printing those it will print default values for those fields.

In case of

Calendar calendar = new GregorianCalendar();

this will create a new instance of calendar with the current time and date values and you are using those static final variables to get that particular value from the newly created calendar instance.

you can check documentation for Calender here

UPDATE

Below is the code for get method, when you pass those static fields to get the method, it will return value for that field from fields array for that index.

public int get(int field)
{
    complete();
    return internalGet(field);
}

protected final int internalGet(int field)
{
    return fields[field];
}

Upvotes: 3

Related Questions