469,315 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

Resolving a non-static cannot be referenced from a static context error

I'm trying to get the day of month with:

int dayofmonth;
dayofmonth = Calendar.get(Calendar.DAY_OF_MONTH);

and I get the compile time error of saying get(int) is a not-static method.

Is there a process (or a different class/method to call) that will work?

Thanks, John
Jul 17 '05 #1
3 24956
On Mon, 29 Sep 2003 12:57:45 -0700, John Bowling wrote:
I'm trying to get the day of month with:

int dayofmonth;
dayofmonth = Calendar.get(Calendar.DAY_OF_MONTH);

and I get the compile time error of saying get(int) is a not-static method.

Is there a process (or a different class/method to call) that will work?

Thanks, John

Hi John,

When get is not defined as static this means that you have to create a
GregorianCalendar object and call get on that; I guess this is because the
creators of the class wanted programmers to be able to give the calendar
their own layout.

Here is an example on how to set up a GregorianCalendar object (most is from Java's API):

String[] ids = TimeZone.getAvailableIDs(1 * 60 * 60 * 1000);
if (ids.length == 0)
System.exit(0);
SimpleTimeZone pdt = new SimpleTimeZone(1 * 60 * 60 * 1000, ids[0]);
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
GregorianCalendar calendar = new GregorianCalendar(pdt);
calendar.setTime(new java.util.Date());

int dayofmonth = calendar.get(Calendar.DAY_OF_MONTH);

Best Regard
Kristian

Jul 17 '05 #2

"Kristian Bisgaard Lassen" <kr***@daimi.au.dk> wrote in message
news:pa****************************@daimi.au.dk...
On Mon, 29 Sep 2003 12:57:45 -0700, John Bowling wrote:
I'm trying to get the day of month with:

int dayofmonth;
dayofmonth = Calendar.get(Calendar.DAY_OF_MONTH);

and I get the compile time error of saying get(int) is a not-static method.
Is there a process (or a different class/method to call) that will work?

Thanks, John Hi John,

When get is not defined as static this means that you have to create a
GregorianCalendar object and call get on that; I guess this is because the
creators of the class wanted programmers to be able to give the calendar
their own layout.

Here is an example on how to set up a GregorianCalendar object (most is

from Java's API):
String[] ids = TimeZone.getAvailableIDs(1 * 60 * 60 * 1000);
if (ids.length == 0)
System.exit(0);
SimpleTimeZone pdt = new SimpleTimeZone(1 * 60 * 60 * 1000, ids[0]);
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
GregorianCalendar calendar = new GregorianCalendar(pdt);
calendar.setTime(new java.util.Date());

int dayofmonth = calendar.get(Calendar.DAY_OF_MONTH);

Best Regard
Kristian


That solved it.
Thanks, John
Jul 17 '05 #3

"Kristian Bisgaard Lassen" <kr***@daimi.au.dk> wrote in message
news:pa****************************@daimi.au.dk...
On Mon, 29 Sep 2003 12:57:45 -0700, John Bowling wrote:
I'm trying to get the day of month with:

int dayofmonth;
dayofmonth = Calendar.get(Calendar.DAY_OF_MONTH);

and I get the compile time error of saying get(int) is a not-static method.
Is there a process (or a different class/method to call) that will work?
When get is not defined as static this means that you have to create a
GregorianCalendar object and call get on that; I guess this is because the
creators of the class wanted programmers to be able to give the calendar
their own layout.


(snip)

It can be done with the Calendar class. You can even create the object with
the same name, so that it works as asked.

import java.util.*;

class calendar {
static public void main(String args[]) {
Calendar Calendar=java.util.Calendar.getInstance();
System.out.println(Calendar.get(Calendar.DAY_OF_WE EK));
}

Some people will suggest giving the object reference variable a different
name, though.

-- glen
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Lorenzo Bolognini | last post: by
2 posts views Thread by Gustaf Liljegren | last post: by
27 posts views Thread by Ken Human | last post: by
reply views Thread by Wanderer | last post: by
1 post views Thread by Stephen | last post: by
8 posts views Thread by junky_fellow | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.