static field should be accessed in a static way

  • Thread starter ireallyneedtoknow2007
  • Start date
I

ireallyneedtoknow2007

Hi
I need to get the current time + 15 minutes for a given time zone.

Question: the c.get(...) show warnings such as
"The static field Calendar.HOUR_OF_DAY should be accessed in a static
way"

how do I avoid these warnings? thanks

public String addMinutes(String tz)
{
if (tz.equalsIgnoreCase("E"))tz="US/Eastern";
else if (tz.equalsIgnoreCase("C")) tz="US/Central";
else if (tz.equalsIgnoreCase("M")) tz="US/Mountain";
else if (tz.equalsIgnoreCase("P")) tz="US/Pacific";
else if (tz.equalsIgnoreCase("A")) tz="US/Alaska";
else if (tz.equalsIgnoreCase("H")) tz="US/Hawaii";
DecimalFormat f=new DecimalFormat("00");
Calendar c = Calendar.getInstance(TimeZone.getTimeZone(tz));
c.set
(
Calendar.getInstance(TimeZone.getTimeZone(tz)).get(Calendar.YEAR),
Calendar.getInstance(TimeZone.getTimeZone(tz)).get(Calendar.MONTH)
+1 ,

Calendar.getInstance(TimeZone.getTimeZone(tz)).get(Calendar.DAY_OF_MONTH),

Calendar.getInstance(TimeZone.getTimeZone(tz)).get(Calendar.HOUR_OF_DAY),
Calendar.getInstance(TimeZone.getTimeZone(tz)).get(Calendar.MINUTE)
);
c.add(Calendar.MINUTE,15 );
// warnings appear below.....
String t=f.format(c.get(c.YEAR)) +
f.format(c.get(c.MONTH)) +
f.format(c.get(c.DAY_OF_MONTH)) +
f.format(c.get(c.HOUR_OF_DAY)) +
f.format(c.MINUTE);
return t;
}
 
C

Chase Preuninger

then ccess it in a static way.

//non-static
MyObject o = new MyObject(...);
o.somethingStatic;

//proper static way
MyObject.somethingStatic;
 
H

Hendrik Maryns

(e-mail address removed) schreef:
Hi
I need to get the current time + 15 minutes for a given time zone.

Question: the c.get(...) show warnings such as
"The static field Calendar.HOUR_OF_DAY should be accessed in a static
way"

It’s not c.get() which is the problem, but that which is inside of it.
In the lines above, you do it right: c.get(Calendar.YEAR)
Calendar.getInstance(TimeZone.getTimeZone(tz)).get(Calendar.YEAR), Good.
String t=f.format(c.get(c.YEAR)) +
Bad.

HTH, H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFIBcg4e+7xMGD3itQRApHAAJ9WBuEpu2gKsyeUSDmJiNihLtfPPACfaoHk
Rgx6o+98H79Tl2DSvMBJYPg=
=KWfK
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top