GregorianCalendar question

This is a discussion on GregorianCalendar question within the Java forums in Programming Languages category; 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...

Go Back   Application Development Forum > Programming Languages > Java

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-26-2008, 08:11 AM
jamie.patricks@gmail.com
Guest
 
Default GregorianCalendar question

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
Reply With Quote
  #2  
Old 08-26-2008, 09:06 AM
Roedy Green
Guest
 
Default Re: GregorianCalendar question

On Tue, 26 Aug 2008 05:11:32 -0700 (PDT), jamie.patricks@gmail.com
wrote, quoted or indirectly quoted someone who said :

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

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Reply With Quote
  #3  
Old 08-26-2008, 10:06 AM
Daniele Futtorovic
Guest
 
Default Re: GregorianCalendar question

On 26/08/2008 14:11, jamie.patricks@gmail.com allegedly wrote:
> 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...


> 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



--
DF.
Reply With Quote
  #4  
Old 08-26-2008, 03:26 PM
Arne Vajhøj
Guest
 
Default Re: GregorianCalendar question

Martin F.L.R.Snashall wrote:
> Arne Vajh?j wrote:
>> 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);
>> }

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


Good point.

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

Arne



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"I think all foreigners should stop interfering in the
internal affairs of Iraq."

--- Deputy Offense Secretary Paul Wolfowitz,

Reply With Quote
  #5  
Old 08-26-2008, 06:35 PM
Arne Vajhøj
Guest
 
Default Re: GregorianCalendar question

jamie.patricks@gmail.com wrote:
> 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

Reply With Quote
  #6  
Old 08-26-2008, 08:01 PM
Arne Vajhøj
Guest
 
Default Re: GregorianCalendar question

jamie.patricks@gmail.com wrote:
> 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
Reply With Quote
  #7  
Old 08-26-2008, 08:16 PM
Richard F.L.R.Snashall
Guest
 
Default Re: GregorianCalendar question

Arne Vajhøj wrote:

> 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


Technical detail: If today were Friday, would that get "last
Friday's" date? Do you need more clarification from the OP?
Reply With Quote
  #8  
Old 08-26-2008, 08:23 PM
Arne Vajhøj
Guest
 
Default Re: GregorianCalendar question

Richard F.L.R.Snashall wrote:
> Arne Vajhøj wrote:
>> 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);
>> }

>
> 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
Reply With Quote
  #9  
Old 08-27-2008, 12:08 AM
Knute Johnson
Guest
 
Default Re: GregorianCalendar question

jamie.patricks@gmail.com wrote:
> 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.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Reply With Quote
  #10  
Old 08-27-2008, 08:09 AM
Daniele Futtorovic
Guest
 
Default Re: GregorianCalendar question

On 27/08/2008 06:08, Knute Johnson allegedly wrote:
> jamie.patricks@gmail.com wrote:
>> 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.


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?

--
DF.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 05:40 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.