By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,640 Members | 2,355 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,640 IT Pros & Developers. It's quick & easy.

Calculating week from GregorianCalendar.

P: n/a
I understand that the GregorianCalendar can be used to calculate the week of
year for a date, and after instantiating a GregorianCalendar object with
the no arg constructor, I can infact retrieve the current week (since it
initializes itself to the current date). However, when I try to create a
GregorianCalendar object by specifying a date in the constructor, it
returns an incorrect value for the WEEK_OF_YEAR field? This is demonstrated
by the code below.

Is there something in need to do to 'initialize' this field in the calendar
object? Am I not understanding the use of this object or is there something
I am missing here?

Any help or advice would be greatly appreciated.
Carl.

--- code ---
import java.util.*;

public class WeekTest {

private GregorianCalendar cal;
public WeekTest(){
// this works...
this.cal = new GregorianCalendar();
setupWeek();
}
public WeekTest(int year, int month, int day){
// this does not work?
this.cal = new GregorianCalendar(
year, month, day, 01, 01, 01);
setupWeek();
}
private void setupWeek() {
this.cal.setFirstDayOfWeek(Calendar.SUNDAY);
this.cal.setMinimalDaysInFirstWeek(4);
}

public void show(){
System.out.println(this.cal.toString());
System.out.println("Dare: " + this.cal.get(Calendar.MONTH) +
"-" + this.cal.get(Calendar.DATE) +
"-" + this.cal.get(Calendar.YEAR));
System.out.println("Week: " +
this.cal.get(Calendar.WEEK_OF_YEAR) +
" Year: " + this.cal.get(Calendar.YEAR));
}
public static void main(String[] args){
//this seems to work fine...
WeekTest wc = new WeekTest();
wc.show();

System.out.println();

//this does not !!!
WeekTest wc2 = new WeekTest(2004, 01, 01);
wc2.show();
}
}
--- end-code ---
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
cg_news wrote:
I understand that the GregorianCalendar can be used to calculate the week
of year for a date, and after instantiating a GregorianCalendar object
with the no arg constructor, I can infact retrieve the current week (since
it initializes itself to the current date). However, when I try to create
a GregorianCalendar object by specifying a date in the constructor, it
returns an incorrect value for the WEEK_OF_YEAR field? This is
demonstrated by the code below.

Is there something in need to do to 'initialize' this field in the
calendar object? Am I not understanding the use of this object or is there
something I am missing here?

Any help or advice would be greatly appreciated.
Carl.

--- code ---
import java.util.*;

public class WeekTest {

private GregorianCalendar cal;
public WeekTest(){
// this works...
this.cal = new GregorianCalendar();
setupWeek();
}
public WeekTest(int year, int month, int day){
// this does not work?
this.cal = new GregorianCalendar(
year, month, day, 01, 01, 01);
setupWeek();
}
private void setupWeek() {
this.cal.setFirstDayOfWeek(Calendar.SUNDAY);
this.cal.setMinimalDaysInFirstWeek(4);
}

public void show(){
System.out.println(this.cal.toString());
System.out.println("Dare: " + this.cal.get(Calendar.MONTH)
+
"-" + this.cal.get(Calendar.DATE) +
"-" + this.cal.get(Calendar.YEAR));
System.out.println("Week: " +
this.cal.get(Calendar.WEEK_OF_YEAR) +
" Year: " +
this.cal.get(Calendar.YEAR));
}
public static void main(String[] args){
//this seems to work fine...
WeekTest wc = new WeekTest();
wc.show();

System.out.println();

//this does not !!!
WeekTest wc2 = new WeekTest(2004, 01, 01);
wc2.show();
}
}
--- end-code ---


Nevermind,

Someone already informed me of my error. I was missing the bit about
calendar months being zero based (i.e. 2004, 01, 01 is infact Feb 1, 2004).

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.