Calculating week from GregorianCalendar.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cg_news

    Calculating week from GregorianCalendar.

    I understand that the GregorianCalend ar can be used to calculate the week of
    year for a date, and after instantiating a GregorianCalend ar 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
    GregorianCalend ar 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 GregorianCalend ar cal;


    public WeekTest(){
    // this works...
    this.cal = new GregorianCalend ar();
    setupWeek();
    }


    public WeekTest(int year, int month, int day){
    // this does not work?
    this.cal = new GregorianCalend ar(
    year, month, day, 01, 01, 01);
    setupWeek();
    }


    private void setupWeek() {
    this.cal.setFir stDayOfWeek(Cal endar.SUNDAY);
    this.cal.setMin imalDaysInFirst Week(4);
    }

    public void show(){
    System.out.prin tln(this.cal.to String());
    System.out.prin tln("Dare: " + this.cal.get(Ca lendar.MONTH) +
    "-" + this.cal.get(Ca lendar.DATE) +
    "-" + this.cal.get(Ca lendar.YEAR));
    System.out.prin tln("Week: " +
    this.cal.get(Ca lendar.WEEK_OF_ YEAR) +
    " Year: " + this.cal.get(Ca lendar.YEAR));
    }


    public static void main(String[] args){
    //this seems to work fine...
    WeekTest wc = new WeekTest();
    wc.show();

    System.out.prin tln();

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

    #2
    Re: Calculating week from GregorianCalend ar.

    cg_news wrote:
    [color=blue]
    > I understand that the GregorianCalend ar can be used to calculate the week
    > of year for a date, and after instantiating a GregorianCalend ar 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 GregorianCalend ar 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 GregorianCalend ar cal;
    >
    >
    > public WeekTest(){
    > // this works...
    > this.cal = new GregorianCalend ar();
    > setupWeek();
    > }
    >
    >
    > public WeekTest(int year, int month, int day){
    > // this does not work?
    > this.cal = new GregorianCalend ar(
    > year, month, day, 01, 01, 01);
    > setupWeek();
    > }
    >
    >
    > private void setupWeek() {
    > this.cal.setFir stDayOfWeek(Cal endar.SUNDAY);
    > this.cal.setMin imalDaysInFirst Week(4);
    > }
    >
    > public void show(){
    > System.out.prin tln(this.cal.to String());
    > System.out.prin tln("Dare: " + this.cal.get(Ca lendar.MONTH)
    > +
    > "-" + this.cal.get(Ca lendar.DATE) +
    > "-" + this.cal.get(Ca lendar.YEAR));
    > System.out.prin tln("Week: " +
    >[/color]
    this.cal.get(Ca lendar.WEEK_OF_ YEAR)[color=blue]
    > +
    > " Year: " +
    > this.cal.get(Ca lendar.YEAR));
    > }
    >
    >
    > public static void main(String[] args){
    > //this seems to work fine...
    > WeekTest wc = new WeekTest();
    > wc.show();
    >
    > System.out.prin tln();
    >
    > //this does not !!!
    > WeekTest wc2 = new WeekTest(2004, 01, 01);
    > wc2.show();
    > }
    > }
    > --- end-code ---[/color]

    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).

    Comment

    Working...