From: Roedy Green on
On Wed, 5 May 2010 12:33:15 -0700 (PDT), laredotornado
<laredotornado(a)zipmail.com> wrote, quoted or indirectly quoted someone
who said :

>Hi,
>
>I'm using Java 1.6 on Mac 10.6.3. I'm trying to get the closest
>Sunday before today, unless today is Sunday in which case I don't want
>to change my calendar object. Here is what I have ...

Calendar is not really designed for pure date calculations. It is more
for timestamps.

You will find that problem trivial to solve with BigDate. See
http://mindprod.com/products.html#COMMON11
--
Roedy Green Canadian Mind Products
http://mindprod.com

What is the point of a surveillance camera with insufficient resolution to identify culprits?
From: Stanimir Stamenkov on
Wed, 5 May 2010 12:33:15 -0700 (PDT), /laredotornado/:

> I'm trying to get the closest Sunday before today, unless today is
> Sunday in which case I don't want to change my calendar object. Here
> is what I have ...
> cal.set(Calendar.DAY_OF_MONTH, 1);
> while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
> cal.add(Calendar.DAY_OF_YEAR, -1);
> out.println("\t<!-- iterating through cal:"
> + cal.getTime().toString() + "-->");
> }
> However, this loop is consistently returning a calendar instance that
> is Saturday. Any ideas of something obvious that I'm missing here?

May be something simpler like:

Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime());

cal.add(Calendar.DATE, -7);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.add(Calendar.DATE, 7);
System.out.println(cal.getTime());

Next, If you perform the same operation on the result which is already
Sunday, you will notice it doesn't change:

cal.add(Calendar.DATE, -7);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.add(Calendar.DATE, 7);
System.out.println(cal.getTime());

--
Stanimir
From: Lew on
On 05/06/2010 01:53 PM, Roedy Green wrote:
> Calendar is not really designed for pure date calculations. It is more
> for timestamps.

Calendar is totally and pointedly designed for pure date calculations. How
could you mislead people like that?

Futhermore, running the OP's code here (what little they posted) does not
reproduce the OP's error.

--
Lew
From: Arne Vajhøj on
On 06-05-2010 21:47, Lew wrote:
> On 05/06/2010 01:53 PM, Roedy Green wrote:
>> Calendar is not really designed for pure date calculations. It is more
>> for timestamps.
>
> Calendar is totally and pointedly designed for pure date calculations.
> How could you mislead people like that?

I would say designed for date & time calculations and working fine for
data calculations.

Working fine as in solving the problem. There are some people that
think the API could be improved.

Regarding Roedy, then I assume that his post was primarily just to post
a link to some of his own stuff.

Arne