| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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 |
|
#3
| |||
| |||
| 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. |
|
#4
| |||
| |||
| 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, |
|
#5
| |||
| |||
| 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 |
|
#6
| |||
| |||
| 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 |
|
#7
| |||
| |||
| 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? |
|
#8
| |||
| |||
| 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 |
|
#9
| |||
| |||
| 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 |
|
#10
| |||
| |||
| 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. |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.