Prev: Recovering an eclipse web project
Next: need suggestions to learn Java to become an Freelanceprogrammer
From: laredotornado on 5 May 2010 15:33 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 ... 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? Thanks for your help, - Dave
From: markspace on 5 May 2010 16:12 laredotornado wrote: > However, this loop is consistently returning a calendar instance that > is Saturday. Any ideas of something obvious that I'm missing here? This worked for me: <code> public class CalendarTest { public static void main( String[] args ) { Calendar c = Calendar.getInstance(); System.err.println( Calendar.SUNDAY ); System.err.println( c.get( Calendar.DAY_OF_WEEK ) ); while( c.get( Calendar.DAY_OF_WEEK ) != Calendar.SUNDAY ) { c.roll( Calendar.DAY_OF_WEEK, -1 ); System.err.println( c.get( Calendar.DAY_OF_WEEK ) ); System.err.println( c.getTime() ); } } } </code> <output> run: 1 4 3 Tue May 04 13:10:08 PDT 2010 2 Mon May 03 13:10:08 PDT 2010 1 Sun May 02 13:10:08 PDT 2010 BUILD SUCCESSFUL (total time: 1 second) </output>
From: Lew on 5 May 2010 19:37 laredotornado wrote: >> However, this loop is consistently returning a calendar instance that >> is Saturday. Any ideas of something obvious that I'm missing here? markspace wrote: > This worked for me: > > <code> > public class CalendarTest { > public static void main( String[] args ) > { > Calendar c = Calendar.getInstance(); > System.err.println( Calendar.SUNDAY ); > System.err.println( c.get( Calendar.DAY_OF_WEEK ) ); > while( c.get( Calendar.DAY_OF_WEEK ) != Calendar.SUNDAY ) { > c.roll( Calendar.DAY_OF_WEEK, -1 ); > System.err.println( c.get( Calendar.DAY_OF_WEEK ) ); > System.err.println( c.getTime() ); > } > } > } > </code> > <output> > run: > 1 > 4 > 3 > Tue May 04 13:10:08 PDT 2010 > 2 > Mon May 03 13:10:08 PDT 2010 > 1 > Sun May 02 13:10:08 PDT 2010 > BUILD SUCCESSFUL (total time: 1 second) > </output> The problem there is the suspect definition of 'roll()': "Adds the specified (signed) amount to the specified calendar field without changing larger fields" as opposed to 'add()', which reconciles the other fields. -- Lew
From: Lew on 5 May 2010 20:04 laredotornado wrote: > 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 ... > > cal.set(Calendar.DAY_OF_MONTH, 1); Are you quite certain you indented your code far enough? It's still readable, so I think you didn't. > 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() + "-->"); > } <http://sscce.org/> <http://sscce.org/> <http://sscce.org/> <http://sscce.org/> <http://sscce.org/> <http://sscce.org/> <http://sscce.org/> Gosh darn it! > However, this loop is consistently returning a calendar instance that > is Saturday. Any ideas of something obvious that I'm missing here? The problem, of course, is in the code you refused to show us because you didn't provide an SSCCE. Are you /trying/ to prevent us from helping you? <sscce> package eegee; import java.util.Calendar; import org.junit.Test; import static org.junit.Assert.assertEquals; public class Calendroll { @Test public void test() { Calendar cal = Calendar.getInstance(); cal.set( Calendar.DAY_OF_MONTH, 1 ); findSunday( cal ); assertEquals( Calendar.SUNDAY, cal.get( Calendar.DAY_OF_WEEK )); cal.set( Calendar.YEAR, 1999 ); cal.set( Calendar.MONTH, Calendar.DECEMBER ); cal.set( Calendar.DAY_OF_MONTH, 31 ); findSunday( cal ); assertEquals( Calendar.SUNDAY, cal.get( Calendar.DAY_OF_WEEK )); } private void findSunday( Calendar cal ) { while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) { cal.add(Calendar.DAY_OF_YEAR, -1); } } } </sscce> Works for me (running in JUnit framework). -- Lew
From: markspace on 5 May 2010 20:41 Lew wrote: > The problem there is the suspect definition of 'roll()': > "Adds the specified (signed) amount to the specified calendar field > without changing larger fields" > > as opposed to 'add()', which reconciles the other fields. > True, and I noticed that well after I posted. Still, at least my example compiles, unlike the OP's.
|
Next
|
Last
Pages: 1 2 Prev: Recovering an eclipse web project Next: need suggestions to learn Java to become an Freelanceprogrammer |