| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#11
| |||
| |||
| Daniele Futtorovic wrote: > 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 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 |
|
#12
| |||
| |||
| In comp.lang.java.programmer message <a0f4f0f0-18f9-4662-9bbf-6e9d6a9efb 62@j1g2000prb.googlegroups.com>, Tue, 26 Aug 2008 05:11:32, jamie.patricks@gmail.com 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. -- (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05. Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc. |
|
#13
| |||
| |||
| On 27/08/2008 18:10, Wayne allegedly wrote: > Daniele Futtorovic wrote: >> 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 > > > 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); } -- DF. |
|
#14
| |||
| |||
| Daniele Futtorovic wrote: > 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? > 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. -- 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 |
|
#15
| |||
| |||
| Daniele Futtorovic wrote: > 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? > 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. -- 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 |
|
#16
| |||
| |||
| Knute Johnson wrote: > > 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. > I too find this interesting for some reason. Here's another version of the code: -Wayne <sscce> // This program displays the date last Friday. // A problem is there is no universally agreed definition of // what "Last Friday" means if today is Friday or Saturday. // One definition is "the most recent Friday prior to today". // Another is "the Friday of last week". According to the // first definition, if today is Saturday than "last Friday" // means yesterday, but according to the second definition // it means 8 days ago. (If today is Friday than by any // popular definition "last Friday" was 7 days ago.) // // For the first definition last Friday is reached by going // back 1..7 days to Friday. For the // second you go back to the first day of this week and // continue backward 2 or 3 days to Friday; note that some // people define a week to start on Sunday and others on // Monday. // // Both definitions can be calculated directly but the // loop versions seem easier to understand. import java.util.*; import static java.util.Calendar.*; public class LastFriday { public static void main ( String [] args ) { Calendar cal = new GregorianCalendar(); cal.set( DAY_OF_WEEK, SATURDAY ); // for testing Calendar def1 = priorFriday( (Calendar) cal.clone() ); Calendar def2 = lastWeek( (Calendar) cal.clone() ); System.out.println( "The current date is: " + cal.getTime() ); System.out.println( "Friday prior to today: " + def1.getTime() ); System.out.println( "Friday of last week: " + def2.getTime() ); } // The most recent Friday prior to today: private static Calendar priorFriday ( Calendar cal ) { cal.add( DAY_OF_MONTH, -1 ); // In case today is Friday while( cal.get(DAY_OF_WEEK) != FRIDAY ) cal.add( DAY_OF_WEEK, -1 ); return cal; } // Friday of last week: private static Calendar lastWeek ( Calendar cal ) { cal.set( DAY_OF_WEEK, cal.getFirstDayOfWeek() ); while ( cal.get( DAY_OF_WEEK ) != FRIDAY ) cal.add ( DAY_OF_WEEK, -1 ); return cal; } } </sscce> |
|
#17
| |||
| |||
| Dr J R Stockton <jrs@merlyn.demon.co.uk> wrote: >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 |
|
#18
| |||
| |||
| >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 |
|
#19
| |||
| |||
| On 28/08/2008 04:00, Knute Johnson allegedly wrote: > Daniele Futtorovic wrote: >> On 27/08/2008 06:08, Knute Johnson allegedly wrote: >>> 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? >> > > 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? ![]() -- DF. |
|
#20
| |||
| |||
| Daniele Futtorovic wrote: > On 28/08/2008 04:00, Knute Johnson allegedly wrote: >> Daniele Futtorovic wrote: >>> On 27/08/2008 06:08, Knute Johnson allegedly wrote: >>>> 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? >>> >> >> 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? ![]() > 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 :-). -- 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 |
![]() |
| 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.