aspose file tools*
The moose likes Java in General and the fly likes Calendar class methods give weird results Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calendar class methods give weird results" Watch "Calendar class methods give weird results" New topic
Author

Calendar class methods give weird results

Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62

Hello Experts !

This is the first time I am using this class, and I am a Java beginner, so please don't kill me

What is wrong with my code ? Please see comments on indicated lines.

Thank you so so much for your help.

kind regards,
marius

import java.util.Calendar;

public class GetDaysInMonth {

public static void main(String[] args) {

Calendar calendar = Calendar.getInstance();

int year = 2012;

int month = Calendar.JANUARY;

System.out.printf("month is %d\n", month);

int date = 1;

calendar.set(year, month, date);

System.out.println("day of the month: " + calendar.DAY_OF_MONTH);

System.out.println("day of month " + calendar.DATE);
// output : 5
what do they mean by "Field number for get and set indicating the day of the month."
why the result is 5 ?

System.out.println("day of week " + calendar.DAY_OF_WEEK);
// output : 7
why the result is 7 This is Thursday, shouldn't it be 0, the result ?

System.out.println("ordinal number of the day of the week within the current month: " + calendar.DAY_OF_WEEK_IN_MONTH);
// output : 8
why the result is 5 ? Today is 26th ?

System.out.println("day of year: " + calendar.DAY_OF_YEAR);
// output : 6
why the result is 5 ? Today is the 26th day ?
}
}
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

All the values that you are printing out are constants. For example, Calendar.DAY_OF_WEEK is a static constant value 7. They don't represent the actual date. Instead, they are used in the get() and set() methods to indicate what field you're interested in. You should notice that they are fields, not methods, and the Java convention is to use capitals for constant values.

It's all in the Javadocs. But, for instance, to get the day of the month, you use:
Try changing your code to use the various varieties of that, and you'll see the difference.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

this is a very good page (i usually find the javadocs useless if you don't have a basic understanding)

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html
Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62

1. thank you very much Matthew and Wendy ! totally missed the caps thing, I knew it. so this fields act like Ruby's symbols, but why do they have a value since they are only used as a flag ? Wasn't just the name good enough, it had to had a value attached to it too ? Looks strange to me.

2. If I keep the same code and add these lines, it gives me a result of false. How come ? 2 Jan 2012 is after 1 Jan 2011.

Calendar cal2 = Calendar.getInstance();
calendar.set(2011, Calendar.JANUARY, 2);

System.out.println(calendar.after(cal2));

I very very much appreciate your help !

Kind regards,
radu
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Marius Constantin wrote:1. thank you very much Matthew and Wendy ! totally missed the caps thing, I knew it. so this fields act like Ruby's symbols, but why do they have a value since they are only used as a flag ?


Every variable to have a value. If enums had been around when this class was created, they could have used those, but they weren't, so they went with the standard named constants idiom.

2. If I keep the same code and add these lines, it gives me a result of false. How come ? 2 Jan 2012 is after 1 Jan 2011.

Calendar cal2 = Calendar.getInstance();
calendar.set(2011, Calendar.JANUARY, 2);


Because you're not reading the docs again.

The set(field, value) method is not the same as the set(year, month, date) method.
Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62

Jeff Verdegan wrote:
Marius Constantin wrote:1. thank you very much Matthew and Wendy ! totally missed the caps thing, I knew it. so this fields act like Ruby's symbols, but why do they have a value since they are only used as a flag ?


Every variable to have a value. If enums had been around when this class was created, they could have used those, but they weren't, so they went with the standard named constants idiom.

2. If I keep the same code and add these lines, it gives me a result of false. How come ? 2 Jan 2012 is after 1 Jan 2011.

Calendar cal2 = Calendar.getInstance();
calendar.set(2011, Calendar.JANUARY, 2);


Because you're not reading the docs again.

The set(field, value) method is not the same as the set(year, month, date) method.


Thank you Jeff,

The thing that was really wrong with my code was that I was setting again the calendar1 variable, instead of the cal2 variable, so that I could compare the dates.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Marius Constantin wrote:
Thank you Jeff,


You're welcome.

The thing that was really wrong with my code was that I was setting again the calendar1 variable, instead of the cal2 variable, so that I could compare the dates.


Ah, okay. Not being able to see your whole code, I could only point out the mistake that I could see.

I'm glad you got it sorted out!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calendar class methods give weird results
 
Similar Threads
Help Needed
Substracting days in java
daysOfWeek function implementation
Confusion in understanding DAY_OF_WEEK
Julian to Gregorian date conversion