• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

Gregorian Calendar Help...

I am using the Gregorian Calendar to calculate days of the year, and the answer I always recieve is off by one day.  For example the 203rd day of 2005 is 7/22/2005 yet I keep coming up with 7/23/2005.  What am I doing wrong?  Do I have to set the minimum days per week, or the first day of the week?

This is the method I am using to calculate the date:

    private String calculateDate(int year, int month, int date, int days){
 
        GregorianCalendar gc1 = new GregorianCalendar();
       
        StringBuffer sb = new StringBuffer();  

           
            year = 2005;
            month =0
            date=1
            days=203                                      
           
            // set the calendar up
            gc1.set(year,month,date);
           
            // then calculate the number of days to get the new date
            gc1.add(Calendar.DATE, days);
           
            // put the date into format that can be used.
           
            sb.append(gc1.get(Calendar.MONTH)+1);
            sb.append("/");
            sb.append(gc1.get(Calendar.DAY_OF_MONTH));
            sb.append("/");
            sb.append(gc1.get(Calendar.YEAR));
           
            System.out.println("Return Date: " + sb);                                      
           
            return sb.toString();                    
    }
0
gdlp2004
Asked:
gdlp2004
  • 5
  • 2
1 Solution
 
zzynxSoftware engineerCommented:
Replace

>> gc1.add(Calendar.DATE, days);

by

      gc1.add(Calendar.DATE, days-1);


0
 
zzynxSoftware engineerCommented:
You start at 1 january (that's the first day)
You add 203 days which give you the 204th
;°)
0
 
gdlp2004Author Commented:
gc1.add(Calendar.DATE, days-1);

I thought about that, but will that work in every instance?  Including leap years?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
zzynxSoftware engineerCommented:
>> will that work in every instance
Yes. See my explanation.
0
 
gdlp2004Author Commented:
It works just like the month I see.  Ok I guess I was making this harder than it is.  Thanks.  
0
 
zzynxSoftware engineerCommented:
Thanks for accepting.

I think you could also replace
>> gc1.add(Calendar.DATE, days);
by
      gc1.set(Calendar.DAY_OF_YEAR, days);


0
 
zzynxSoftware engineerCommented:
>> It works just like the month I see
No, that's not the point.

To reach the 203th day of the year you should
- ADD 202 days to the first
or
- SET DAY_OF_YEAR to 203
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now