Solved

# Gregorian Calendar Help...

Posted on 2005-04-29
428 Views
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

// 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
Question by:gdlp2004

LVL 37

Accepted Solution

Replace

by

0

LVL 37

Expert Comment

You start at 1 january (that's the first day)
You add 203 days which give you the 204th
;°)
0

Author Comment

I thought about that, but will that work in every instance?  Including leap years?
0

LVL 37

Expert Comment

>> will that work in every instance
Yes. See my explanation.
0

Author Comment

It works just like the month I see.  Ok I guess I was making this harder than it is.  Thanks.
0

LVL 37

Expert Comment

Thanks for accepting.

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

0

LVL 37

Expert Comment

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

### Suggested Solutions

nested class vs inner class 5 36
word0 challenge 4 37
create a gui in perl 3 22
JAVA part two 5 11
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.