GregorianCalendar question

J

jamie.patricks

Hi,
I would like to get last Friday's date. Can you please check if there
is any issue with the code below? Thanks.

GregorianCalendar calendar = new GregorianCalendar();
// set to last week
calendar.set( Calendar.WEEK_OF_YEAR ,
calendar.get(Calendar.WEEK_OF_YEAR)-1 );
// set to Friday
calendar.set( Calendar.DAY_OF_WEEK, Calendar.FRIDAY );

Regards,
Jamie
 
R

Roedy Green

I would like to get last Friday's date. Can you please check if there
is any issue with the code below? Thanks.

what happens when you try it? What happens around year end e.g. Jan
1.
 
D

Daniele Futtorovic

Hi,
I would like to get last Friday's date. Can you please check if there
is any issue with the code below? Thanks.

It won't work on Saturdays. Then again, neither do I...
 
A

Arne Vajhøj

I would like to get last Friday's date. Can you please check if there
is any issue with the code below? Thanks.

GregorianCalendar calendar = new GregorianCalendar();
// set to last week
calendar.set( Calendar.WEEK_OF_YEAR ,
calendar.get(Calendar.WEEK_OF_YEAR)-1 );
// set to Friday
calendar.set( Calendar.DAY_OF_WEEK, Calendar.FRIDAY );

As other have states then there are potential barrages.

I would go for the deep:

while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}

Arne


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Voice or no voice, the people can always be brought to
the bidding of the leaders. That is easy. All you have
to do is tell them they are being attacked and denounce
pacifists for lack of patriotism and exposing the country
to danger.

It works the same way in any country.

--- Herman Goering (second in command to Adolf Hitler)
at the Nuremberg Trials
 
A

Arne Vajhøj

I would like to get last Friday's date. Can you please check if there
is any issue with the code below? Thanks.

GregorianCalendar calendar = new GregorianCalendar();
// set to last week
calendar.set( Calendar.WEEK_OF_YEAR ,
calendar.get(Calendar.WEEK_OF_YEAR)-1 );
// set to Friday
calendar.set( Calendar.DAY_OF_WEEK, Calendar.FRIDAY );

As other have states then there are potential problems.

I would go for the simple:

while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}

Arne
 
A

Arne Vajhøj

Richard said:
Technical detail: If today were Friday, would that get "last
Friday's" date? Do you need more clarification from the OP?

Good point.

Very likely he may want to roll one back before the while loop.

Arne
 
K

Knute Johnson

Hi,
I would like to get last Friday's date. Can you please check if there
is any issue with the code below? Thanks.

GregorianCalendar calendar = new GregorianCalendar();
// set to last week
calendar.set( Calendar.WEEK_OF_YEAR ,
calendar.get(Calendar.WEEK_OF_YEAR)-1 );
// set to Friday
calendar.set( Calendar.DAY_OF_WEEK, Calendar.FRIDAY );

Regards,
Jamie

Contrary to all the responses you got, I don't see a problem. This will
give you a GregorianCalendar set to last Friday at the time you created
it. I don't see any problems with Saturday or the end of the year. I
use code like this all the time without any problems.

Some places use different days for the start and end of a week. In the
US, a week starts on Sunday and ends on Saturday. That could give
varying results depending on the default locale of your system but I
would assume if you were in France you would want the week of your
program to start on Monday.
 
D

Daniele Futtorovic

Contrary to all the responses you got, I don't see a problem. This will
give you a GregorianCalendar set to last Friday at the time you created
it. I don't see any problems with Saturday or the end of the year. I
use code like this all the time without any problems.

Interesting. Let's hypothesise without testing for the sake of the
mental exercise:

I don't see any problem either for end of year -- provided the Calendar
is lenient. But, let's assume the Calendar is set to WEEK_OF_YEAR x and
to Saturday. "Last Friday" would have been one day before. Regardless of
whether the first day of the week is defined as Sunday or Monday, the
call altering the WEEK_OF_YEAR will set the Calendar to Saturday of week
x-1, that is seven days before. The call to set DAY_OF_WEEK would set
the date to Friday /in week x-1/, ergo one day further back.
Consequently, compared to its original status, the Calendar would have
been wound back eight days, and not, as it should, one.

Where's the fault in that reasoning?
 
D

Dr J R Stockton

In comp.lang.java.programmer message <a0f4f0f0-18f9-4662-9bbf-6e9d6a9efb
(e-mail address removed)>, Tue, 26 Aug 2008 05:11:32,
(e-mail address removed) posted:
I would like to get last Friday's date.

Last Friday is reached by going back 7 days then forward 0..6 days to
Friday.

Java appears to contain, to the relevant extent, similar routines to
those in JavaScript, in which I would use the following illustrative
algorithm. It requires appropriate behaviour for setDate(X) for X<1.

Friday = 5
D = new Date()
D.setDate(D.getDate() - 7 + (7 + Friday - D.getDay())%7)

I suppose the newer Java Methods have similar capability.
 
D

Daniele Futtorovic

Daniele said:
It won't work on Saturdays. Then again, neither do I...


I think this will work:

<sscce>
// Sample code to get last Friday's date.

import java.util.*;
import static java.util.Calendar.*;

public class LastFriday {
public static void main (String [] args ) {
GregorianCalendar cal = new GregorianCalendar();
Date now = cal.getTime();
if ( cal.get( DAY_OF_WEEK ) == FRIDAY )
cal.add( WEEK_OF_YEAR , -1 );
cal.set( DAY_OF_WEEK, FRIDAY );
if ( cal.getTimeInMillis() > System.currentTimeMillis() )
cal.add( WEEK_OF_YEAR , -1 );
Date lastFri = cal.getTime();

System.out.println( "The time now is: " + now );
System.out.println( "Last Friday was: " + lastFri );
}
}
</sscce>

-Wayne

Shorter:

static boolean THIS_FRIDAY_IS_LAST_FRIDAY;

Calendar cal = ...;

long oldtime = cal.getTimeInMillis();

cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);

if( THIS_FRIDAY_IS_LAST_FRIDAY ? (cal.getTimeInMillis() > oldtime) :
(cal.getTimeInMillis() >= oldtime) )
{
cal.add(Calendar.WEEK_OF_YEAR, -1);
}
 
K

Knute Johnson

Daniele said:
Interesting. Let's hypothesise without testing for the sake of the
mental exercise:

I don't see any problem either for end of year -- provided the Calendar
is lenient. But, let's assume the Calendar is set to WEEK_OF_YEAR x and
to Saturday. "Last Friday" would have been one day before. Regardless of
whether the first day of the week is defined as Sunday or Monday, the
call altering the WEEK_OF_YEAR will set the Calendar to Saturday of week
x-1, that is seven days before. The call to set DAY_OF_WEEK would set
the date to Friday /in week x-1/, ergo one day further back.
Consequently, compared to its original status, the Calendar would have
been wound back eight days, and not, as it should, one.

Where's the fault in that reasoning?

The fault is in the definition of "last Friday." To me, last Friday is
the Friday in the week prior to this one even if it is Saturday. So if
you think last Friday is yesterday then I see the problem.
 
K

Knute Johnson

Daniele said:
Interesting. Let's hypothesise without testing for the sake of the
mental exercise:

I don't see any problem either for end of year -- provided the Calendar
is lenient. But, let's assume the Calendar is set to WEEK_OF_YEAR x and
to Saturday. "Last Friday" would have been one day before. Regardless of
whether the first day of the week is defined as Sunday or Monday, the
call altering the WEEK_OF_YEAR will set the Calendar to Saturday of week
x-1, that is seven days before. The call to set DAY_OF_WEEK would set
the date to Friday /in week x-1/, ergo one day further back.
Consequently, compared to its original status, the Calendar would have
been wound back eight days, and not, as it should, one.

Where's the fault in that reasoning?

This is actually very interesting thing. I looked around the internet
for the expression "last someday" and came up with the English language
structure but no description of what "last Friday" means. I always
assumed that last Friday really meant the same as Friday last week.
Last week being the week prior to this one. But it could easily be
interpreted as you stated, the most recently past Friday.
 
D

Dirk Michaelsen

Dr J R Stockton said:
Last Friday is reached by going back 7 days then forward 0..6 days to
Friday.

Why then not simply going back day by day until you reach a Friday?

GregorianCalendar cal = new GregorianCalendar();
Date now = cal.getTime();
while (cal.get( DAY_OF_WEEK ) != FRIDAY)
cal.add(DAY_OF_YEAR, -1);

Dirk
 
D

Dirk Michaelsen

Why then not simply going back day by day until you reach a Friday?
GregorianCalendar cal = new GregorianCalendar();
Date now = cal.getTime();
while (cal.get( DAY_OF_WEEK ) != FRIDAY)
cal.add(DAY_OF_YEAR, -1);

sorry, you don't really need 'now' ;-)

Dirk
 
D

Daniele Futtorovic

The fault is in the definition of "last Friday." To me, last Friday
is the Friday in the week prior to this one even if it is Saturday.
So if you think last Friday is yesterday then I see the problem.

Culprit identified, then.

This is actually very interesting thing. I looked around the internet
for the expression "last someday" and came up with the English language
structure but no description of what "last Friday" means. I always
assumed that last Friday really meant the same as Friday last week. Last
week being the week prior to this one. But it could easily be
interpreted as you stated, the most recently past Friday.

Luckily, all natural languages I'm aware of will give us means to
resolve the ambiguity.

But it is indeed interesting that what you would have naturally assumed
is the contrary of what I (and as a matter of fact a few others) would
have naturally assumed. I wonder what the reason is.

To try to outlay my reasoning, firstly I don't really see any practical
usefulness to getting the Friday of the week prior to this or that
systematically. In other words I struggle to envision a use-case. Since
you say you have coded thusly before, you will doubtlessly be able to
enlighten me as to this.
Secondly, if "last Friday" is Friday of the week prior to this one, then
"last spring" is the spring of the year prior to this, last equinox the
equinox of the year prior to this, and so forth. In other words, this
definition hinges of a particular cycle (you must know which week/year
you're in) -- whereas the other doesn't. In my understanding, "last
something" is the first something starting from now (or whatever the
point of reference is) and going backwards in time.
Also, if "last spring" is the spring of last year ("last year" being the
first year the end of which is encountered by going backwards in time
from now), then there might be a spring between now and the one that's
supposed to be the "last"...

So where did you get those silly ideas of yours? ;)
 
K

Knute Johnson

Daniele said:
Culprit identified, then.



Luckily, all natural languages I'm aware of will give us means to
resolve the ambiguity.

But it is indeed interesting that what you would have naturally assumed
is the contrary of what I (and as a matter of fact a few others) would
have naturally assumed. I wonder what the reason is.

To try to outlay my reasoning, firstly I don't really see any practical
usefulness to getting the Friday of the week prior to this or that
systematically. In other words I struggle to envision a use-case. Since
you say you have coded thusly before, you will doubtlessly be able to
enlighten me as to this.
Secondly, if "last Friday" is Friday of the week prior to this one, then
"last spring" is the spring of the year prior to this, last equinox the
equinox of the year prior to this, and so forth. In other words, this
definition hinges of a particular cycle (you must know which week/year
you're in) -- whereas the other doesn't. In my understanding, "last
something" is the first something starting from now (or whatever the
point of reference is) and going backwards in time.
Also, if "last spring" is the spring of last year ("last year" being the
first year the end of which is encountered by going backwards in time
from now), then there might be a spring between now and the one that's
supposed to be the "last"...

So where did you get those silly ideas of yours? ;)

It is very possible that it is just a colloquialism used by my tribe.
On the other hand what you call the Friday of the next week if it was
Thursday today? I would call that next Friday as opposed to this Friday.

Or it was all caused by my parents dropping me on my head when I was
very small :).
 
D

Dr J R Stockton

In comp.lang.java.programmer message <qsgcb45r42lv5o0trmso6m25fpuvektvjp
Why then not simply going back day by day until you reach a Friday?

That depends on whether you are prepared to waste CPU time by using an
inefficient approach, just to save a little careful thinking.

If instead one wants the Friday of the previous week, a similar-but-
different calculation will do it - the thinking goes via the first day
of the current week or the last day of the previous week, and from there
to the desired Friday, but the program arithmetic is combined.

Especially in business, last Friday is Friday last week; but last Monday
is usually after Friday last week.

For anything truly involving calendar weeks, one needs to consider
whether standard weeks or USA/Church (or, maybe, weeks of other faiths)
are required.
 
J

jrobinss

[non-Java one-time posting, my bad]

On the other hand what you call the Friday of the next week if it was
Thursday today? I would call that next Friday as opposed to this Friday.

For what it's worth, there is a French colloquialism for this case.

If it's Thursday and you mean to say Friday next week, you say "Friday
in eight" ("vendredi en huit").
If you mean tomorrow, you say tomorrow ("demain").
If you say next Friday, and if you're talking to me, I'll probably
answer, what do you mean, tomorrow or Friday in eight? :)

So there you are, it's a common enough issue that at least one
language has developped its own specific expression. :)

BTW, I'd probably use the "go back until it's Friday" approach,
because
1. I'm sure it works
2. any sub-developer can understand it (especially if the method is
well named and commented :) )
3. premature optimization blahblahblah
 
D

Dr J R Stockton

In comp.lang.java.programmer message <[email protected]
sdemon.com>, Thu, 28 Aug 2008 09:11:51, Knute Johnson <nospam@rabbitbrus
h.frazmtn.com> posted:
It is very possible that it is just a colloquialism used by my tribe.
On the other hand what you call the Friday of the next week if it was
Thursday today? I would call that next Friday as opposed to this
Friday.

In English English, one tends to avoid using terms such as "Last Friday"
or "Next Friday" on days when misinterpretation seems possible; and the
better-informed avoid using "last week" or "next week" on a Sunday,
unless referring to seven days ago/ahead. It is always possible to
express them differently or to give more detail, if the possible
ambiguity is not otherwise resolved (for example, as in "last week's
monthly meeting").

The OP's question was not as clear as it could have been.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top